C# / .NET equivalent for Java Collections.emptyList()?

后端 未结 8 2082
孤街浪徒
孤街浪徒 2020-12-29 02:11

What\'s the standard way to get a typed, readonly empty list in C#, or is there one?

ETA: For those asking \"why?\": I have a virtual method that re

相关标签:
8条回答
  • 2020-12-29 02:50

    You can just create a list:

    List<MyType> list = new List<MyType>();
    

    If you want an empty IEnumerable<T>, use Enumerable.Empty<T>():

    IEnumerable<MyType> collection = Enumerable.Empty<MyType>();
    

    If you truly want a readonly list, you could do:

    IList<MyType> readonlyList = (new List<MyType>()).AsReadOnly();
    

    This returns a ReadOnlyCollection<T>, which implements IList<T>.

    0 讨论(0)
  • 2020-12-29 02:52
    IList<T> list = new List<T>().AsReadOnly();
    

    Or, if you want an IEnumerable<>:

    IEnumerable<T> sequence = Enumerable.Empty<T>();
    
    0 讨论(0)
  • 2020-12-29 02:55

    Starting with .net 4.6 you can also use:

    IList<T> emptyList = Array.Empty<T>();
    

    This does only create a new instance once for every different type you specify as T.

    0 讨论(0)
  • 2020-12-29 02:57

    Construct an instance of System.Collections.ObjectModel.ReadOnlyCollection from your list.

    List<int> items = new List<int>();
    ReadOnlyCollection<int> readOnlyItems = new ReadOnlyCollection<int>(items);
    
    0 讨论(0)
  • 2020-12-29 03:03

    Personally, I think this is better than any of the other answers:

    static readonly IList<T> EmptyList = new T[0];
    
    • Arrays implement IList<T>.
    • You cannot add to an array.
    • You cannot assign to an element in an empty array (because there is none).
    • This is, in my opinion, a lot simpler than new List<T>().AsReadOnly().
    • You still get to return an IList<T> (if you want).

    Incidentally, this is what Enumerable.Empty<T>() actually uses under the hood, if I recall correctly. So theoretically you could even do (IList<T>)Enumerable.Empty<T>() (though I see no good reason to do that).

    0 讨论(0)
  • 2020-12-29 03:08

    To expand on Dan Tao's answer, the following implementation can be used in the same way as Enumerable.Empty<T>(), by specifying List.Empty<T>() instead.

    public static class List
    {
        public static IList<T> Empty<T>()
        {
            // Note that the static type is only instantiated when
            // it is needed, and only then is the T[0] object created, once.
            return EmptyArray<T>.Instance;
        }
    
        private sealed class EmptyArray<T>
        {
            public static readonly T[] Instance = new T[0];
        }
    }
    

    Edit: I change the above code to reflect the outcome of a discussion with Dan Tao about lazy versus eager initialization of the Instance field.

    0 讨论(0)
提交回复
热议问题