Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不包含';的公共实例定义;getenumerator';对于每个抛出的错误_C# - Fatal编程技术网

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
     }
    }