为什么在ReadOnlyCollection上使用ImmutableList? [英] Why use ImmutableList over ReadOnlyCollection?

查看:40
本文介绍了为什么在ReadOnlyCollection上使用ImmutableList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 4.5具有新的命名空间 System.Collections不可变

.NET 4.5 has a new namespace System.Collections.Immutable

此软件包提供了线程安全的集合,并保证永不更改其内容,也称为不可变集合.

This package provides collections that are thread safe and guaranteed to never change their contents, also known as immutable collections.

我很困惑. ReadOnlyCollection 并不能解决线程安全问题班级?为什么使用 ImmutableList 代替?

I'm confused. Isn't the thread safety problem already solved by the ReadOnlyCollection class? Why use ImmutableList instead?

我知道还有一个 IReadOnlyList 接口 .但这并不能隐式解决线程安全问题,因为其他线程可能会通过另一个接口来编辑对象.

I know there's also an IReadOnlyList interface. That doesn't solve the thread safety problem implicitly, because other threads may edit the object by another interface.

推荐答案

使用

只读的集合只是带有包装器的集合防止修改集合;因此,如果进行了更改对于基础集合,只读集合反映了那些变化.

A collection that is read-only is simply a collection with a wrapper that prevents modifying the collection; therefore, if changes are made to the underlying collection, the read-only collection reflects those changes.

使用 ImmutableList 不会发生这种情况.

This can't happen with an ImmutableList.

这篇关于为什么在ReadOnlyCollection上使用ImmutableList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆