tags:

views:

58

answers:

4

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'.

+9  A: 

The instances are the same if they are classes, but copies if they are structs/valuetypes.

int, byte, double are value types and any structs (like System.Drawing.Point and self defined structs). But strings, all of your own classes, basically "the rest" is reference types.

Note: LINQ uses the same rules as all other assignments.

For objects:

Person p1 = new Person();
p1.Name = "Mr Jones";
Person p2 = p1;
p2.Name = "Mr Anderssen";
// Now p1.Name is also "Mr Anderssen"

For structs

Point p1 = new Point();
p1.x = 5;
Point p2 = p1;
p2.x = 10;
// p1.x is still 5

The same rules applies when using LINQ.

Albin Sunnanbo
That is what I had presumed. I just wanted to make sure that the Where expression was not doing some sort of shallow copying on my class instances.
Brandon
Worth noting that anonymous types are always classes, so references as well.
Richard
@Richard: Also worth noting that anonymous types are immutable in C# so you can't modify an anonymous type instance anyway.
LukeH
+2  A: 

They are same objects. Where only filters, Select produces (can produce) new instances.

Andrey
A: 

Making a new object that is a reference type is non-trivial. LINQ would have no idea how to do it. LINQ always returns the same instances when dealing with reference types.

Matt Greer
A: 

I just wanted to add to some of the other answers -- in general, when I'm not sure of something but require a particular behavior, I'll add a unit test for it. You could easily put this into a test and then check for equality, which will tell you if you're looking at a reference of the object in the original container. Some may argue that this is stupid because you "should just know" what happens, but for me I know I will either be 1) unsure because I'm not an awesome programmer, and 2) there are always nights where I have to burn the midnight oil, and it's good to have the reassurance that something behaves as you need it to.

Dave
This question stemmed because when I had a collection that I was filtering using the Where expression, my Entity Framework Self Tracking Entities were not behaving correctly. I believe the problem is not due to my misunderstanding of how C# handles references, but a problem with tracking being turned off after performing the Where expression.
Brandon