Is it better to return null or empty collection?

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

    Think always in favor of your clients (which are using your api):

    Returning 'null' very often makes problems with clients not handling null checks correctly, which causes a NullPointerException during runtime. I have seen cases where such a missing null-check forced a priority production issue (a client used foreach(...) on a null value). During testing the problem did not occur, because the data operated on was slightly different.

提交回复
热议问题