C# 不包含';的公共实例定义;getenumerator';对于每个抛出的错误
我试图使用C# 不包含';的公共实例定义;getenumerator';对于每个抛出的错误,c#,C#,我试图使用foreach方法循环遍历包含学生数据的列表,但是我得到的错误是QA不包含每个的“getenumerator”的公共实例定义 我的质量保证等级如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Test { class QA {
foreach
方法循环遍历包含学生数据的列表,但是我得到的错误是QA不包含每个
的“getenumerator”的公共实例定义
我的质量保证等级如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Test
{
class QA
{
private List<Student> students;
public QA()
{
students = new List<Student>();
string line;
using (StreamReader reader = new StreamReader("/Users/jvb/Desktop/Students.txt"))
{
line = reader.ReadLine();
var s = line.Split(',');
int id = int.Parse(s[0]);
int houseNo = int.Parse(s[3]);
var status = int.Parse(s[7]);
Student sData = new Student(id, s[1], s[2], houseNo, s[4], s[5], s[6], (StudentStatus)status);
AddStudent(sData);
}
}
public List<Student> GetStudents()
{
return students;
}
public void AddStudent(Student student)
{
students.Add(student);
}
}
}
我是c#的新手,谁能解释一下我误解了什么/做错了什么吗?您是不可枚举的直接使用对象,您必须访问IList实现的可枚举it成员 你完全做错了 您正在迭代不可iterable的类对象。你不需要付小费
static void Main(string[] args)
{
QA students = new QA();
var studentList= s.GetStudents(); //you get all the students not you can iterate on this lidt
foreach(var student in studentList)
{
//here you can access student property like
Console.WriteLine(student.Name); //I assume Name is a property of Student class
}
}
这修复了错误,但我无法访问
GetStudents()
方法?@JayVB您正在执行此操作。。。学生们。GetStudents()对吗?哦,我明白了,这就解决了问题。对不起,我误会了。谢谢你!
static void Main(string[] args)
{
QA students = new QA();
var studentList= s.GetStudents(); //you get all the students not you can iterate on this lidt
foreach(var student in studentList)
{
//here you can access student property like
Console.WriteLine(student.Name); //I assume Name is a property of Student class
}
}