Given a class that holds (and initializes once) an IEnumerable<T>
:
public class MyClass
{
public MyClass()
{
Values = Sequence(0, 10).Select(i => Convert(i));
}
public IEnumerable<string> Values { get; private set; }
private static string Convert(int i)
{
string iStr = i.ToString();
Console.WriteLine("Convert: " + iStr);
return iStr;
}
private static IEnumerable<int> Sequence(int start, int end)
{
return Enumerable.Range(start, end - start + 1);
}
}
Why does every usage that requires the evaluated values of myClass.Values
re-evaluates them?
e.g.:
static void Main(string[] args)
{
MyClass myClass = new MyClass();
foreach (var v in myClass.Values)
{
Console.WriteLine(v);
}
foreach (var v in myClass.Values)
{
Console.WriteLine("Value: " + v);
}
}
This will print 20 times "Convert: " and "Value: ".
I know that saving the Values as a list (with ToList()
for example) will solve this, but it was my understanding that once Values are evaluated the values will be used from memory (as if I used Lazy<T>
and then Values.value
), why is this not the same case?