c# – Is it better to return null or an empty collection?

Question:

If the return type is a collection, which is better: return null or an empty collection? Is there a common practice?

Similar question on en.SO: Is it better to return null or empty collection?

Answer:

The issue here is semantics.

To the request "give me a list of all users", an empty collection has an obvious meaning: "ok, here's a list of all users, there are exactly 0 of them."

On the other hand, the returned null could mean anything: "I don't know how many users", "the number of users hasn't been counted yet", "the current database has no concept of _user_" at all, whatever.

The meaning of an empty collection is always clear, the meaning of null is undefined.

Related article: Eric Lippert, Null Is Not Empty .

Scroll to Top