简体   繁体   中英

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#?

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

foreach (var course in courses)

Once you have that, the inner loop is very simple, just access Faculty (its already 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.

If you change the loop variable to a Course then you can access the properties.

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

I hope that helped!

EDIT
I took too long writing this and missed your edit that you'd like a foreach-loop. See other answers for that. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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