Is it bad form to return Arrays in C#? Should I return List?

后端 未结 7 1079
南方客
南方客 2020-12-14 20:31

I have a function which returns a variable number of elements, should I return an array or a List? The \"collection\'s\" size does not change once returned, ie for all purpo

7条回答
  •  臣服心动
    2020-12-14 21:13

    One opinion I see pretty often a suggestion to return either IList or ReadOnlyCollection. It's OK to return these if you have one of these available - both can be assigned directly to an IEnumerable (they work directly with any LINQ methods). One thing to note is ReadOnlyCollection is a very lightweight type that can wrap any IList.

提交回复
热议问题