Is it better to return null or empty collection?

前端 未结 18 1689
我在风中等你
我在风中等你 2020-11-22 07:56

That\'s kind of a general question (but I\'m using C#), what\'s the best way (best practice), do you return null or empty collection for a method that has a collection as a

18条回答
  •  太阳男子
    2020-11-22 08:14

    From the Framework Design Guidelines 2nd Edition (pg. 256):

    DO NOT return null values from collection properties or from methods returning collections. Return an empty collection or an empty array instead.

    Here's another interesting article on the benefits of not returning nulls (I was trying to find something on Brad Abram's blog, and he linked to the article).

    Edit- as Eric Lippert has now commented to the original question, I'd also like to link to his excellent article.

提交回复
热议问题