简体   繁体   English

C#中的只读列表

[英]Read-Only List in C#

I have some class with List -property:我有一些带有List -property 的类:

class Foo {
  private List<int> myList;
}

I want provide access to this field only for read.我想提供对该字段的访问权限,仅供阅读。

Ie I want property with access to Enumerable, Count, etc. and without access to Clear, Add, Remove, etc. How I can do it?即我想要属性可以访问 Enumerable、Count 等,但不能访问 Clear、Add、Remove 等。我该怎么做?

You can expose a List<T> as a ReadOnlyCollection<T> by using the AsReadOnly() method您可以使用AsReadOnly()方法将List<T>公开为ReadOnlyCollection<T>

C# 6.0 and later (using Expression Bodied Properties ) C# 6.0 及更高版本(使用Expression Bodied Properties

class Foo { 

  private List<int> myList;

  public ReadOnlyCollection<int> ReadOnlyList => myList.AsReadOnly();

}

C# 5.0 and earlier C# 5.0 及更早版本

class Foo {

  private List<int> myList;

  public ReadOnlyCollection<int> ReadOnlyList {
     get {
         return myList.AsReadOnly();
     }
  }
}

If you want a read-only view of a list you could use ReadOnlyCollection<T> .如果您想要列表的只读视图,您可以使用ReadOnlyCollection<T>

class Foo {
    private ReadOnlyCollection<int> myList;
}

i would go for我会去

public sealed class Foo
{
    private readonly List<object> _items = new List<object>();

    public IEnumerable<object> Items
    {
        get
        {
            foreach (var item in this._items)
            {
                yield return item;
            }
        }
    }
}

There is now an Immutable Collections library that does exactly that.现在有一个不可变集合库可以做到这一点。 Which you can install via nuget.您可以通过 nuget 安装。

The immutable collection classes are supported starting with the .NET Framework 4.5.从 .NET Framework 4.5 开始支持不可变集合类。

https://msdn.microsoft.com/en-us/library/dn385366%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/dn385366%28v=vs.110%29.aspx

The System.Collections.Immutable namespace provides generic immutable collection types that you can use for these scenarios, including: ImmutableArray<T>, ImmutableDictionary<Tkey,TValue>, ImmutableSortedDictionary<T>, ImmutableHashSet<T>, ImmutableList<T>, ImmutableQueue<T>, ImmutableSortedSet<T>, ImmutableStack<T> System.Collections.Immutable 命名空间提供可用于这些场景的通用不可变集合类型,包括:ImmutableArray<T>、ImmutableDictionary<Tkey,TValue>、ImmutableSortedDictionary<T>、ImmutableHashSet<T>、ImmutableList<T>、ImmutableQueue <T>、ImmutableSortedSet<T>、ImmutableStack<T>

Example of Usage:用法示例:

class Foo
{
    public ImmutableList<int> myList { get; private set; }

    public Foo(IEnumerable<int> list)
    {
        myList = list.ToImmutableList();
    }
}

If you declare a readonly list in your class, you'll still be able to add items to it.如果您在类中声明只读列表,您仍然可以向其中添加项目。

If you don't want to add or change anything, you should be using ReadOnlyCollection<T> as Darin suggested.如果您不想添加或更改任何内容,则应按照 Darin 的建议使用ReadOnlyCollection<T>

If you want to add, remove items from the list but don't want to change the content, you can use a readonly List<T> .如果您想从列表中添加、删除项目但不想更改内容,您可以使用readonly List<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM