繁体   English   中英

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

[英]Accessing an item that's inside of a list in C#

我有一个清单

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)
            {

            }
        }

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

使用类类型:

foreach (Course course in courses)

或为喜欢的地方var (这是类型推断var被替换Course在引擎盖下,因为这是对泛型类型参数courses 。在不同的情况下会选择不同的类型......事关于单独学习)

foreach (var course in courses)

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

foreach(Faculty f in course.Faculty)

您的代码的主要问题是以下行:

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)
    {

    }
}

您需要将创建的类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  

也许此文档可以帮助您进一步:
微软物业
MSDN上的集合

希望对您有所帮助!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM