Call a method in the declaration of a foreach

c#, foreach

Solution

There's nothing wrong with that. The method will only be evaluated once.

It is basically:

using(var iter = GetItemDetails(item).GetEnumerator())
{
    while(iter.MoveNext()
    {
        var item = iter.Current;
        // ...
    }
}

Problem

I have a foreach which calls a method to get its collection. ``` foreach(var item in GetItemDetails(item)) { } ``` Visual studio doesn't complain about this, nor does Resharper, however, before I continue to use this approach, I want to reach out and check if it is a recommended approach.

Original source

Related problems