Is it better to return null or empty collection?

前端 未结 18 1571
我在风中等你
我在风中等你 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:13

    There is one other point that hasn't yet been mentioned. Consider the following code:

        public static IEnumerable GetFavoriteEmoSongs()
        {
            yield break;
        }
    

    The C# Language will return an empty enumerator when calling this method. Therefore, to be consistant with the language design (and, thus, programmer expectations) an empty collection should be returned.

提交回复
热议问题