c# - 列表和只读属性

标签 c# list readonly-collection

List(和 List)实例可以是只读的,参见 ReadOnly 属性;在集合具有 ReadOnly 属性的情况下,方法会抛出异常。

如何创建只读列表实例?主要用途是什么?

最佳答案

ReadOnlyCollection

来自上面的链接:

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

因此,无论何时您需要一个不可更改的集合,都可以使用它。

关于c# - 列表和只读属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2924646/

相关文章:

c# - DataTable 到复杂嵌套 JSON

c# - 如何在两个脚本中使用 bool 来决定是否调用函数?

java - 如何将AbstrDoubleList的内容写入TextArea?

c# - 如何防止方法调用者修改返回的集合?

c# - Sql Server 以编程方式从远程客户端还原/备份数据库

c# - 如何实例化一个窗口而不显示它?

c# - linq查询中的自动编号

python - Django:更新查询集中对象的顺序属性

c# - 什么是只读集合?

c# - Collection 底层的 ReadOnlyCollection 属性在迭代时被修改