Q:

C# convert a for loop to for each (arraylist)

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);
    }
}

Edit
I need to  cast the returned object from the ArrayList to a Student object like the above.

A:

You should always keep trying to figure out these kinds of beginner issues, but here's the answer to your question:
public void ListStudents()
{
    foreach(Student student in studentArrayList)
    {
        Console.WriteLine("Student {0} {1}", student.FirstName, student.LastName);
    }
}

A:

As others have noted, if all the objects are of type Student, then you can just use the for loop without a cast, and you should use a List (or anything other than ArrayList) instead.
If for some reason you are storing objects of types other than Student in your studentsArrayList, then you can get just the Student types by doing something like:
foreach (var someObject in studentsArrayList)
{
    // Try to cast the object as a Student
    var studentCast = someObject as Student;

    // If it's not null, the cast succeeded
    if (studentCast != null)
    {
        Console.WriteLine("{0} {1}", studentCast.FirstName, studentCast.LastName);
    }
}

Or using Linq:
foreach (Student student in studentsArrayList.ToArray()
    .Where(s => s.GetType() == typeof (Student)))
{
    Console.WriteLine("{0} {1}", student.FirstName, student.LastName);
}

