I have an array of struct in an object. I want to pass each of these struct in the array to the class using a foreach loop so that I can display each struct in the class. Here is the code in the object to pass the struct :
public IEnumerator recup_voitures()
{
foreach (voiture v in _arrVCollection)
{
yield return (v);
}
}
and I try to reccuperate these in the class using :
foreach (CarCollection.voiture o in collection.recup_voitures())
{
//some console.writeline to display whats inside each struct
}
this dosen't work. I get an error that say that it dosen't have a public GetEnumerator. Can anyone help me to figure this out?
You need to return an IEnumerable. (preferably IEnumerable<T>
)
foreach
operates on (objects that look like) IEnumerable<T>
and calls GetEnumerator()
to enumerate over the collection.