C# 不可变集合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/927181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:24:17  来源:igfitidea点击:

Immutable collections?

c#.netimmutability

提问by Joan Venge

I am making most of my basic types in my app, immutable. But should the collections be immutable too? To me, this seems like a huge overhead unless I am missing something.

我在我的应用程序中制作了大部分基本类型,不可变。但是集合也应该是不可变的吗?对我来说,这似乎是一个巨大的开销,除非我遗漏了什么。

I am talking about collections to hold Point3 values, etc which can be added as it goes at different times. So if there are 1M values in a collection, and you needed to delete 1 of them, you would have to create the same collection all over again, right?

我说的是保存 Point3 值等的集合,这些值可以在不同时间添加。因此,如果一个集合中有 100 万个值,而您需要删除其中的 1 个,则必须重新创建相同的集合,对吗?

回答by n8wrl

It all depends on who is using the collections at the same time. Strings are immutable to prevent boo-boo's like two threads trying to remove the first char at the same time.

这完全取决于谁同时使用这些集合。字符串是不可变的,以防止像两个线程一样试图同时删除第一个字符。

回答by Steef

If you have a collection to which you can add items after constructing it, it is not immutable

如果你有一个可以在构建后添加项目的集合,它不是一成不变的

回答by Dan Blair

A look up table would make for a decent immutable collection. It doesn't need to change in size and you want it static so it's quick to look up tricky calculations. If you need to add something later then I wouldn't bother with immutability, it defeats the purpose.

查找表将构成一个体面的不可变集合。它不需要改变大小,并且您希望它是静态的,因此可以快速查找棘手的计算。如果您稍后需要添加一些内容,那么我不会为不变性而烦恼,它违背了目的。

回答by Marc Gravell

If you only ever add/remove from the start or end you might be able to cheat - but in general; yes: the implication is that you need to create a new collection for every change.

如果您只从开头或结尾添加/删除,您可能会作弊 - 但总的来说;是的:这意味着您需要为每次更改创建一个新集合。

So: do you need to (effectively) mutate collections? If so, and given their size: I'd be tempted to look at synchronizing access (rather than making them properly immutable). Look at lock(aka Monitor).

所以:你需要(有效地)改变集合吗?如果是这样,并且考虑到它们的大小:我很想考虑同步访问(而不是使它们正确地不可变)。看看lock(又名Monitor)。

回答by Bill K

My favorite trick with collections is simply to never pass them around. If they only exist inside a single object, then making them immutable is mostly irrelevant (As long as your containing object doesn't change them then they won't change).

我最喜欢的收藏技巧就是永远不要传递它们。如果它们只存在于单个对象中,那么使它们不可变几乎是无关紧要的(只要您的包含对象不改变它们,它们就不会改变)。

Usually your collection represents something, right? It's a collection of dogs or a collection of invoices...

通常你的收藏代表一些东西,对吧?是狗的集合还是发票的集合...

Usually there is a thing you can do with a collection of dogs (Herd? neuter?) or a collection of invoices (pay?) There are virtually always operations that apply to the whole list of objects--operations that have functionality beyond the singular invoice.pay() (for instance, ensuring that the most important invoices are paid first), without a class around your collection, there is really no where to put those operations.

通常,你可以用一组狗(牧群?中性?)或一组发票(支付?)来做一件事。几乎总是有一些操作适用于整个对象列表——操作具有超越单数的功能invoice.pay()(例如,确保首先支付最重要的发票),如果没有围绕您的集合的类,那么实际上没有地方放置这些操作。

It also usually makes sense to have a few variables associated with your collection--and again without a wrapper you always end up putting those variables in some strange unnatural location.

将一些变量与您的集合相关联通常也是有意义的——而且如果没有包装器,您总是最终将这些变量放在一些奇怪的不自然的位置。

It may seem strange at first but try it a couple times before you judge.

一开始可能看起来很奇怪,但在你判断之前尝试几次。

回答by Dario

It depends on the style your program is written/designed.

这取决于您的程序编写/设计的风格。

Immutable collection do only make sense when you're programming in a functional-programming-influenced style (Imperatively designed programs shouldn't use them).

不可变集合只有在您以受函数式编程影响的风格进行编程时才有意义(命令式设计的程序不应该使用它们)。

And like in functional languages, you should use Linked Lists then which can be built up in O(1) per element (cons) and process them functionally (recursions, building new lists from lists).

就像在函数式语言中一样,你应该使用链表,然后它可以在每个元素 ( cons) 的O(1) 中构建并在功能上处理它们(递归,从列表构建新列表)。

When your program requires imperative collections (arrays, vectors/lists), keep them mutable.

当您的程序需要命令式集合(数组、向量/列表)时,请保持它们可变。

回答by LBushkin

I agree with Eric's comments about choosing the right tool for the problem. Immutability adds value when your goals include providing clear identity semantics, or making your implementation easier to work with in a parallel computing environment. Immutability can also help improve performance by allowing optimizations such as caching or transparent proxying.

我同意 Eric 关于为问题选择正确工具的评论。当您的目标包括提供清晰的身份语义或使您的实现更易于在并行计算环境中使用时,不变性会增加价值。不变性还可以通过允许优化(例如缓存或透明代理)来帮助提高性能。

On the flip-side, immutability can also incur a performance cost - particularly when you use the "copy-on-write" pattern to model "changes".

另一方面,不变性也会导致性能成本 - 特别是当您使用“写时复制”模式来模拟“更改”时。

You have to decide whyyou want your entities/collections to be immutable - and that will help drive your decision of whether to do so or not.

你必须决定为什么你希望你的实体/集合是不可变的——这将有助于推动你决定是否这样做。

回答by James L

You could define your public interface as IEnumerable, but still use a mutable collection in your implementation.

您可以将公共接口定义为 IEnumerable,但仍然在您的实现中使用可变集合。

回答by Andrew Arnott

Immutable collections are great, especially if your app already leverages immutable types or semantics.

不可变集合很棒,特别是如果您的应用程序已经利用了不可变类型或语义。

.NET just shipped their first immutable collections, which I suggest you try out.

.NET 刚刚发布了他们的第一个不可变集合,我建议您尝试一下。