Given the following code:
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
//Init data
char[] chars = new char[10];
FillData(chars);
// Write the initial data
PrintContents("Initial data:", chars);
//Take some data:
IEnumerable<char> acc = chars.Take(3);
//View data
PrintContents("Enum:", acc);
//Edit data
chars[0] = 'z';
chars[1] = 'z';
chars[2] = 'z';
//View data again
PrintContents("Enum after modifing source:", acc);
//Restart data
chars = new char[5];
FillData(chars);
//View data when source is replaced
PrintContents("Enum after new source:", acc);
}
//Gets a ref
private static void FillData(char[] data)
{
for(int i = 0; i < data.Length; i++)
{
data[i] = (char)('a' + i);
}
}
private static void PrintContents(string what, IEnumerable<char> src)
{
System.Console.WriteLine(what);
string s = "";
foreach(char ch in src)
{
s += ch;
}
if(s.Length > 0)
{
System.Console.WriteLine(s);
}
}
}
I get this output:
Initial data:
abcdefghij
Enum:
abc
Enum after modifing source:
zzz
Enum after new source:
zzz
I know about the deferred execution, but is that the expected behaivour? This means I should ever reuse an IEnumerable or any data used on an IEnumerable without creating a new collection as I may change the results on a program.
This means that the IEnumerable will hold a reference to the data sources as well even if they are not used by the visible code as well and will not be Garbage Collected until the IEnumerable itself is to be collected.
I have been using IEnumerable a lot on a recent project and the more I see them the less I like them. Don't take me wrong, Linq does a great job but I would prefer it to sometimes return the same type of the source.