c# covert a for loop to for each (arraylist)
-
hi, I have a for loop that works but I need to implement a foreach loop. How do I change to a foreach loop ? here is the existing working for loop in a method
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); } }
Also, does the above code indeed cast the returned object from the ArrayList to a Student object ?
-
hi, I have a for loop that works but I need to implement a foreach loop. How do I change to a foreach loop ? here is the existing working for loop in a method
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); } }
Also, does the above code indeed cast the returned object from the ArrayList to a Student object ?
Aindriu Mac Giolla Eoin wrote:
change to a foreach loop
Why?
For
loops perform better.Aindriu Mac Giolla Eoin wrote:
does the above code indeed cast the returned object
Looks like it; why do you ask?
-
Aindriu Mac Giolla Eoin wrote:
change to a foreach loop
Why?
For
loops perform better.Aindriu Mac Giolla Eoin wrote:
does the above code indeed cast the returned object
Looks like it; why do you ask?
I need a foreach loop to satisfy a requirement even though a for loop might perform better. I was looking at [microsoft link] but could get the cast work as well I wanted to double check that the returned object was casted to its orignal type thanks !
-
hi, I have a for loop that works but I need to implement a foreach loop. How do I change to a foreach loop ? here is the existing working for loop in a method
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); } }
Also, does the above code indeed cast the returned object from the ArrayList to a Student object ?
Try this:
foreach (Student currentStudent in studentarraylist)
{
Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
}You should try to avoid the
ArrayList
class[^], as it's not strongly typed. Use theList<T>
class[^] (or one of the other generic collection types) instead. Generics (C# Programming Guide)[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Try this:
foreach (Student currentStudent in studentarraylist)
{
Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
}You should try to avoid the
ArrayList
class[^], as it's not strongly typed. Use theList<T>
class[^] (or one of the other generic collection types) instead. Generics (C# Programming Guide)[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
thanks for that, i know its not strongly typed and could use something else but I need to use it for a question.
-
hi, I have a for loop that works but I need to implement a foreach loop. How do I change to a foreach loop ? here is the existing working for loop in a method
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); } }
Also, does the above code indeed cast the returned object from the ArrayList to a Student object ?
While a for loop may perform better, a foreach loop reads better and is less prone to off-by-one errors. That is why I have come to prefer them everywhere except _proven_ performance bottlenecks.
foreach (Student currentStudent in studentarraylist)
{
Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
}