, GetAllTerminals(), , . , , .
The simplest solution, as you mentioned, is to copy the result of the call before performing any other operations. If you wanted to, you could neatly wrap this behavior in IEnumerable<T>, which makes an internal enumerated call only once:
public class CachedEnumerable<T> : IEnumerable<T>
{
public CachedEnumerable<T>(IEnumerable<T> enumerable)
{
result = new Lazy<List<T>>(() => enumerable.ToList());
}
private Lazy<List<T>> result;
public IEnumerator<T> GetEnumerator()
{
return this.result.Value.GetEnumerator();
}
System.Collections.IEnumerable GetEnumerator()
{
return this.GetEnumerator();
}
}
Wrap the result in an instance of this type and it will not evaluate the internal enumerable multiple times.
source
share