To assign IEnumerable to a new instance of IEnumerable, you will need ToList method. Here is an example:
IEnumerable instanceOne; IEnumerable instanceTwo; List instanceListOne = new List(); instanceListOne.Add("a"); instanceListOne.Add("b"); instanceOne = instanceListOne; instanceTwo = instanceOne; instanceListOne.Add("c"); foreach (string instance in instanceTwo) { Console.WriteLine(instance); } Console.WriteLine("Press any key"); Console.ReadKey(); instanceTwo = instanceOne.ToList(); instanceListOne.Add("d"); foreach (string instance in instanceTwo) { Console.WriteLine(instance); } Console.WriteLine("Press any key"); Console.ReadKey();
Notice first:
instanceListOne.Add("a"); instanceListOne.Add("b"); instanceOne = instanceListOne; instanceTwo = instanceOne;
Then:
instanceListOne.Add("c");
I added firs "a" and "b" to instanceListOne, I assigned instanceListOne to instanceOne, and then I assigned instanceOne to instanceTwo, after that I added "c" to instanceListOne, then I went through instanceTwo and result is:
"c" is displayed also in instanceTwo.
Now to have two separate instances of IEnumerable, we need something like:
instanceTwo = instanceOne.ToList();
After that I did something like:
instanceListOne.Add("d");
Result will be still as in previous picture, "d" is not added to instanceTwo, since now we really have separate instances.
Example download from here.