简体   繁体   English

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

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

I have a list 我有一个清单

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

And Courses looks like this, 课程看起来像这样,

 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).";
    }
}

Im trying to access the List faculty inside of of the list course so I can traverse and print the contents of it. 我正在尝试访问列表课程中的列表老师,因此我可以遍历并打印列表内容。 How would I go about doing this in C#? 我将如何在C#中执行此操作?

Below is how im trying to access it in another class. 下面是我如何尝试在另一个类中访问它。

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

            }
        }

First off, don't use object as your iteration variable type. 首先,不要使用object作为迭代变量类型。 You won't be able to access any class specific members... and its just weird. 您将无法访问任何特定于班级的成员……这太奇怪了。

Use the class type: 使用类类型:

foreach (Course course in courses)

or for places that like var (this is type inference , var gets replaced by Course under the hood because that is the generic type argument of courses . In different contexts it will choose the type differently... something to learn about separately) 或为喜欢的地方var (这是类型推断var被替换Course在引擎盖下,因为这是对泛型类型参数courses 。在不同的情况下会选择不同的类型......事关于单独学习)

foreach (var course in courses)

Once you have that, the inner loop is very simple, just access Faculty (its already public ) 一旦有了它,内部循环就非常简单,只需访问Faculty (已经public

foreach(Faculty f in course.Faculty)

The main issue with your code is this line: 您的代码的主要问题是以下行:

foreach(object o in course)

The loop variable o is actually a Course , but since it is of type Object it does not know about its properties unless you cast it. 循环变量o实际上是Course ,但是由于它是Object类型的,因此除非您进行强制转换,否则它不会知道其属性。

If you change the loop variable to a Course then you can access the properties. 如果将循环变量更改为“ 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)
    {

    }
}

You need to make the class you've created public to access it's internal fields and variables. 您需要将创建的类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).";
    }
}

Now if you create some courses and put them in your list you can access them like this: 现在,如果您创建了一些课程并将它们放在列表中,则可以按以下方式访问它们:

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

Maybe this documentation can help you further: 也许此文档可以帮助您进一步:
Microsoft on properties 微软物业
MSDN on collections MSDN上的集合

I hope that helped! 希望对您有所帮助!

EDIT 编辑
I took too long writing this and missed your edit that you'd like a foreach-loop. 我花了太长时间写了这篇,却错过了您想要一个foreach循环的编辑。 See other answers for that. 看到其他答案。 :) :)

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

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