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