C#将for循环转换为每个(arraylist)

时间:2015-04-27 19:53:57

标签: c#

我有一个for循环,但我需要实现一个foreach循环。如何更改为foreach循环?

以下是方法中现有的for循环

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对象。

7 个答案:

答案 0 :(得分:4)

你应该一直试图弄清楚这些初学者问题,但这里是你问题的答案:

public void ListStudents()
{
    foreach(Student student in studentArrayList)
    {
        Console.WriteLine("Student {0} {1}", student.FirstName, student.LastName);
    }
}

答案 1 :(得分:2)

foreach (Student student in studentarraylist)
{
    Console.Writeline("Student {0} {1}", student.FirstName, student.LastName);
}

它到底是什么here

  

我需要将返回的对象从ArrayList转换为Student对象,如上所述

使用foreach循环的全部目的是让你不必投射对象,或者更准确地说你是在它所说的部分进行的

foreach (Student student in studentarraylist //here)

答案 2 :(得分:2)

正如其他人所说,如果所有对象都是Student类型,那么你可以在没有强制转换的情况下使用for循环,你应该使用List(或任何东西)而不是ArrayList)。

如果出于某种原因,您要在Student中存储studentsArrayList以外的其他类型的对象,那么您可以通过执行以下操作来获得Student类型的对象:

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

或使用Linq:

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

答案 3 :(得分:1)

foreach(var element in studentarraylist){
    var currentStudent = (Student) element;
    Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName);
}

答案 4 :(得分:1)

foreach(Student student in studentarraylist)
 {
    Console.WriteLine("Student {0} {1}", student.FirstName, student.LastName);
 }

答案 5 :(得分:1)

仅当每个对象都是学生时才有效。它会尝试将每个元素投射到学生身上。如果您不关心这一点,这很好。

someword:    .asciiz     "want this as a char*"

更好的方法是完全删除foreach(Student currentStudent in studentarraylist) { Console.WriteLine("Student {0} {1}", currentStudent.FirstName, currentStudent.LastName); } ,而使用ArrayList

List<Student>

还有一个列表,你可以使用一些LINQ,如果你的话。

var studentsList = new List<Student>();
studentsList.Add(new Student { FirstName = "Aaron", LastName = "Rodgers" });

foreach (var student in studentsList)
{
   Console.WriteLine("Student {0} {1}", student.FirstName, student.LastName);
}

答案 6 :(得分:1)

正如其他人所说,如果所有对象都是Student类型,那么你可以在没有强制转换的情况下使用for循环,你应该考虑使用List(或取而代之的是ArrayList)以外的任何东西。

如果出于某种原因,您要在Student中存储studentsArrayList以外的其他类型的对象,那么您可以通过执行以下操作来获得Student类型的对象:

foreach (var someObject in students)
{
    // 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);
    }
}

或使用Linq:

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