I have a homework assigment to iterate through an object array and print out these objects using for
and foreach
. I'm stuck on how to do that.
Questions
When you use a foreach loop, don't you have to declare the object? So the object declared in a foreach
loop is null, because it doesn't call any constructors in my Employee
class.
Code Snippet
while ((worker = Employee.ReadFromFile(employeeDataReader)) != null)
{
employeeInfo[j] = worker;
j++;
}
foreach (Employee person in employeeInfo)
{
person.Print();
}
How do I print out the objects contained in an array? Am I 'doing it wrong'? Is there a better way?