Вам нужно создать созданный вами класс 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 по свойствам
MSDN по коллекциям
Я надеюсь, что это помогло!
РЕДАКТИРОВАТЬ
Я слишком долгонаписав это и пропустив вашу правку, вы захотите цикл foreach.Смотрите другие ответы для этого.:)