I am manually converting Java to C# and have the following code:
for (Iterator<SGroup> theSGroupIterator = SGroup.getSGroupIterator();
theSGroupIterator.hasNext();)
{
SGroup nextSGroup = theSGroupIterator.next();
}
Is there an equivalent of Iterator<T>
in C# or is there a better C# idiom?
The direct equivalent in C# would be IEnumerator<T>
and the code would look something like this:
SGroup nextSGroup;
using(IEnumerator<SGroup> enumerator = SGroup.GetSGroupEnumerator())
{
while(enumerator.MoveNext())
{
nextSGroup = enumerator.Current;
}
}
However the idiomatic way would be:
foreach(SGroup group in SGroup.GetSGroupIterator())
{
...
}
and have GetSGroupIterator
return an IEnumerable<T>
(and probably rename it to GetSGroups()
or similar).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With