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

后端 未结 8 2083
孤街浪徒
孤街浪徒 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 03:08

    What about:

    readonly List<T> mylist = new List<T>();
    

    Not sure why you want it readonly; that doesn't make much sense in most scenarios I can think of, though.

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

    If you want a list whose contents can't be modified, you can do:

    ReadOnlyCollection<Foo> foos = new List<Foo>().AsReadOnly();
    
    0 讨论(0)
提交回复
热议问题