This is probably a basic question for some, but it affects how I design a piece of my program.
I have a single collection of type A:
IEnumerable<A> myCollection;
I am filtering my collection on 2 different criteria:
IEnumerable<A> subCollection1 = myCollection.Where(x => x.Count > 10);
etc.
Now, I know that the .Where expression will return a new instance of IEnumerable, but does the new collection contain the same reference to an instance of type A that ‘myCollection’ references, or are new copies of type A created? If new instances of type ‘A’ are created, is there a way to say that ‘subCollection1’ references the same instances of A as ‘myCollection’ references?
Edit: To Add further clarification.
I am looking for a way so that when I make a change to an instance of ‘A’ in ‘subCollection1’, that it is also modified for ‘myCollection’.
The instances are the same if they are classes, but copies if they are structs/value types.
int, byte and double are value types, as are structs (like
System.Drawing.Pointand self-defined structs).But strings, all of your own classes, basically “the rest”, are reference types.
Note: LINQ uses the same rules as all other assignments.
For objects:
For structs:
The same rules apply when using LINQ.