Return empty list <T> or null if there are no list items?

When I have a method that returns a collection of objects, what should I return if the number of objects is zero? null or just empty List<T> ? What is good practice?

 public List<string> GetPupilsByClass(string className) { .... } 
+7
source share
9 answers

I would definitely return an empty list, so methods can still be called on an object without requiring a zero check. There is a difference between returning an empty list and returning nothing at all, so the calling code probably does not expect to receive a null reference in any case (unless there is any exception).

+15
source

This depends on a number of factors, but an empty list will be a more typical return value, because otherwise the caller must know to perform a null check. The main point when I would return null is a method of this style:

 bool Try*(args, out result) 

The caller expects (when receiving false ) not to even look at the value of result .

If you, it turns out, return arrays, there is a good cheat - you can store an array with zero length in a static field somewhere returned. But ultimately, an empty list will not be a huge overhead, so just post it.

+3
source

An empty list is what I expect as a caller. Null tells me that the "conceptual list" is undefined, like null in the database.

Also, always returning empty collections, rather than null, clients like these never fail:

 foreach(var element in obj.Method()) ... 
+3
source

According to MS, you should never return an empty string or array from a field or property, and I think this could be extended to methods (and maybe somewhere that I did not find).

When returning empty arrays:

The String and Array properties should never return a null reference. Zero can be difficult to understand in this context.

The general rule is that null, empty strings ("") and empty (0 elements) arrays should be processed the same way. Return an empty array instead of a null reference.

MSDN link .

+2
source

Is this method an ifc method? What does it mean to use external objects not controlled by you? if the answer is yes, then I will return an empty collection since the caller does not expect an exception.

if this method is internal and you are the only user, I would return zero to save the lost memory allocated for the empty collection and GC performance when you stop using it.

+1
source

Best practice is to return an IEnumerable<string> . Use yield return and yield break in your method to create your collection. This way you delay the creation of the array. Check here for more information. You will find that IEnumerable has the advantage that it can be bound in extension methods and linq queries:

 var results = from x in GetPupilsByClass(className) where x.StartsWith("A"); 

If you absolutely need to return the full list (due to the lazy nature of yield ), I would recommend changing your method signature to the following:

 public bool TryGetPupilsByClass(string className, out ICollection<string> pupils) 

This method has three advantages.

  • Your intentions are clear - the students will be initialized if the return value is true. The user of your code should not guess what practice you have set.
  • Of course, you don’t bother to select a list if the collection is empty, which saves memory allocation.
  • ICollection<string> strongly typed without revealing the storage mechanism used. Returning specific classes should be used sparingly. Alternatives are things like IList<string> , ReadOnlyCollection<string> and IEnumerable<string> , but the one you choose certainly makes your intentions about what the user can and cannot do with the results much clearer.
+1
source

Allways returns an emty list. And you will avoid the most common exceptions - NullReferenceException .

+1
source

It depends on the context in which your code is used. For most purposes, I am returning a list of null objects, and I think that would be best here - it is more in line with your normal results.

0
source

I usually follow the guidelines of the R. R. Martin Clean Code. It is recommended that you return an empty list:

open list GetPupilsByClass (string className) {....}

I do this for the following reasons:

  1. The calling method should not check for zero.
  2. This method should not do error handling.
  3. Exceptions will be detected on a higher level method.
0
source

All Articles