LINQ创建返回时是否新建内存

发布于 2024-10-13 19:05:42 字数 63 浏览 3 评论 0原文

LINQ 实际上是否将结果深层复制到不同的列表/数组/等,或者它只是给我一个列表/数组/等。由对原文的引用组成?

Does LINQ actually perform a deep copy of the results to a different list/array/etc, or does it simply give me a list/array/etc. composed of references to the original?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

另类 2024-10-20 19:05:42

这将取决于您是否(以及如何)使用“选择”来预测结果。

如果您不在投影中创建新对象,则结果将引用与原始集合相同的对象。

但是,如果您在项目中创建新对象,那么显然它们将不一样。

此处返回的集合将包含对 _myCollection 中相同对象的引用:

from m in _myCollection
where m.SomeFilterCriteria
select m

在这些情况下返回的集合不会:

from m in _myCollection
where m.SomeFilterCriteria
select new { m.Prop1, m.Prop2 }

在这种情况下,值得指出的是新匿名对象的 Prop1 和 Prop2 - 如果它们是引用类型 - 将包含对与原始对象相同的对象的引用。只有集合中的顶级引用会有所不同。

基本上 - 除了序列化器(如这里其他地方提到的)之外,.Net 中没有任何东西会“深度”复制,除非你实现它。

或者

from m in _myCollection
where m.SomeFilterCriteria
select m.Clone()

再次强调,假设此处发生任何“深度”复制是错误的。当然,Clone 的实现将在类中,并且可以是任何内容,包括深度复制,但没有给出。

It's going to depend on if (and how) you use Select to project the results.

If you do not create new objects in a projection then the result will reference the same objects as the original collection.

If, however, you create new objects in the project then, obviously, they will not be the same.

The collection returned here will contain references to the same objects in _myCollection:

from m in _myCollection
where m.SomeFilterCriteria
select m

The collections returned in these cases will not:

from m in _myCollection
where m.SomeFilterCriteria
select new { m.Prop1, m.Prop2 }

In this case, it is worth pointing out that Prop1 and Prop2 of the new anonymous object - if they are reference types - will contain a reference to the same object as the original object. Only the top-level references in the collection will be different.

Basically - nothing in .Net aside from serializers (as mentioned elsewhere here) will "deep" copy, unless you implement it.

or

from m in _myCollection
where m.SomeFilterCriteria
select m.Clone()

Again, it would be a mistake to assume that any "deep" copying is going on here. Of course, Clone's implementation will be in the class and could be anything, including deep copying, but that is not given.

倾听心声的旋律 2024-10-20 19:05:42

LINQ 实际上是否将结果深层复制到不同的列表/数组/等,或者它只是给我一个列表/数组/等。由对原文的引用组成?

来自Enumerable.ToArray。 (在 Enumerable.ToList 中找到类似的文本)

ToArray(IEnumerable) 方法强制立即计算查询并返回包含查询结果的数组。您可以将此方法附加到您的查询中,以获得查询结果的缓存副本。

嗯,这看起来确实令人困惑。

  1. 返回包含查询结果的数组
  2. 获取查询结果的缓存副本

从第一句中可以清楚地看出,没有创建查询中的项目副本

从第二句话中,您将获得整个查询结果的副本,但这是一个浅副本,因为没有创建查询中的项目副本

Does LINQ actually perform a deep copy of the results to a different list/array/etc, or does it simply give me a list/array/etc. composed of references to the original?

From Enumerable.ToArray. (Similiar text found at Enumerable.ToList)

The ToArray(IEnumerable) method forces immediate query evaluation and returns an array that contains the query results. You can append this method to your query in order to obtain a cached copy of the query results.

Well, that certainly looks confusing.

  1. returns an array that contains the query results
  2. obtain a cached copy of the query results

From the first sentence, it is clear that no copies of items in the query are made.

From the second sentence, you get a copy of the query results as a whole, but that is a shallow copy since no copies of items in the query are made.

浸婚纱 2024-10-20 19:05:42

它返回的内容很大程度上取决于您所引用的 LINQ 方法。但除了少数显式复制枚举的方法(例如 ToListToArray)之外,一般模式是不将输入复制到新结构。相反,它更喜欢惰性评估。

What it returns is very dependent on which LINQ method you are referring to. But with the exception of the few methods which explicitly copy the enumeration (ToList and ToArray for example) the general pattern is to not copy the input to a new structure. Instead it prefers lazy evaluation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文