我有一個for循環工作,但我需要實現一個foreach循環。如何更改爲foreach循環?C#轉換for循環爲每個(arraylist)
這裏是循環現有工作中的方法
public void liststudents()
{
for (int i = 0; i < studentarraylist.Count; i++)
{
// Cast the object from the ArrayList to Student
Student currentStudent = (Student)studentarraylist[i];
Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
}
}
編輯
我需要從ArrayList中投返回的對象像上面的一個Student對象。
將'for'循環轉換爲'foreach'之前,將'ArrayList'轉換爲'List',不要在2015年使用'ArrayList'。 –
Habib
嘿,我知道循環更好,但我需要實現一個foreach循環。它的學術...我檢查https://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx,但無法讓它工作 – Aindriu
它應該不那麼難。你有嘗試過什麼嗎? –