访问C#中列表内部的项目

时间:2018-11-07 19:28:16

标签: c# list

我有一个清单

public static List<Courses> course = new List<Courses>();

课程看起来像这样

 class Courses
{
    public string courseName { get; set; }
    public List<Faculty> faculty { get; set; }
    public List<Student> student { get; set; }

    public override string ToString()
    {  
        return courseName + ", which features " + faculty.Count + ", faculty member(s) and " + student.Count + " student(s).";
    }
}

我正在尝试访问列表课程中的列表老师,因此我可以遍历并打印列表内容。我将如何在C#中执行此操作?

下面是我如何尝试在另一个类中访问它。

 //print stuff
        int courseCounter = 0, studentCounter = 0, facultyCounter = 0;
        //object[] o = course.ToArray()
        foreach(object o in course)
        {
            courseCounter++;
            Console.WriteLine("Course #" + courseCounter + " is " + o.ToString());

            foreach(object f in HOW WOULD I ACCESS IT HERE)
            {

            }
        }

3 个答案:

答案 0 :(得分:4)

首先,请不要使用object作为迭代变量类型。您将无法访问任何特定于班级的成员……这太奇怪了。

使用课程类型:

foreach (Course course in courses)

或对于类似var(这是推断类型)的地方,var在幕后被Course替换,因为这是courses。在不同的情况下,它会选择不同的类型...分别要了解的内容

foreach (var course in courses)

有了它,内部循环就非常简单,只需访问Faculty(它已经是public

foreach(Faculty f in course.Faculty)

答案 1 :(得分:1)

您的代码的主要问题是这一行:

foreach(object o in course)

循环变量o实际上是Course,但是由于它的类型为Object,除非您强制转换,否则它不知道其属性。

如果将循环变量更改为Course,则可以访问属性。

foreach(Courses c in course)
{
    courseCounter++;
    Console.WriteLine("Course #" + courseCounter + " is " + c.ToString());

    foreach(Faculty f in c.faculty)
    {

    }

    foreach(Student s in c.student)
    {

    }
}

答案 2 :(得分:1)

您需要创建自己创建的类public才能访问其内部字段和变量。

public class Courses
{
    //Properties (Begin property with a capital)
    public string CourseName { get; set; }
    public List<Faculty> Faculties { get; set; }
    public List<Student> Students { get; set; }

    public override string ToString()
    {  
        return courseName + ", which features " + faculty.Count + ", faculty member(s) and " + student.Count + " student(s).";
    }
}

现在,如果您创建一些课程并将其放在列表中,则可以像这样访问它们:

//Create a new course or as many as you wish and fill the model class with data.
Course mathCourse = new Course()
{
    CourseName = "Math",
    Faculties = new List<Faculty>(),
    Students = new List<Student>()
;
mathCourse.Faculties.Add(new Faculty("Faculty for math Alpha"));
mathCourse.Faculties.Add(new Faculty("Faculty for math Beta"));
mathcourse.Students.Add(new Student("Ben"));

//create as many different courses as you want and at them to your list you've created.
course.Add(mathCourse);

//you can now reach the faculty in the course like this
//We select with [0] the first course in the courseList, then we select the first faculty of the course with [0] and select its property 'name'.
//This goes for any property as long the class Faculty is public and its property is too.
string facultyName = course[0].Faculties[0].Name;

//>> facultyName => Faculty for math Alpha  

也许此文档可以为您提供进一步的帮助:
Microsoft on properties
MSDN on collections

希望对您有所帮助!

编辑
我花了太长时间写了这篇,却错过了您想要一个foreach循环的编辑。看到其他答案。 :)