LINQ - 请问在哪里EX pression返回新的实例或引用对象实例实例、或引用、对象、LINQ

由网友(喜当爹i)分享简介:这可能是对一些基本的问题,但它会影响我如何设计一件我的程序。This is probably a basic question for some, but it affects how I design a piece of my program.我有一个单一的集合型的:I have a single colle...

这可能是对一些基本的问题,但它会影响我如何设计一件我的程序。

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;

我过滤的2个不同的标准,我的收藏​​:

I am filtering my collection on 2 different criteria:

IEnumerable<A> subCollection1 = myCollection.Where(x => x.Count > 10);
etc.

现在,我知道了。凡EX pression将返回的IEnumerable的新实例,但新的集合包含相同的参照A类的一个实例是MyCollection的参考文献,或者是新副本A型产生的?如果类型的新实例A的创建,是有没有办法说,subCollection1参考A作为MyCollection的'引用相同的实例吗?

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?

编辑:添加进一步澄清

我要寻找一种方式,这样,当我做出改变,以subCollection1''A'的一个实例,它也被修改为MyCollection的。

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/valuetypes.

整型,字节,双是值类型以及任何结构(如 System.Drawing.Point 和自定义的结构)。 但是字符串,全部由您自己的类,基本上是休息是引用类型的。

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.

注:LINQ使用相同的规则,所有其他任务

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

对于对象:

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

有关结构

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

相同的规则使用LINQ时适用。

The same rules applies when using LINQ.

阅读全文

相关推荐

最新文章