Empty Collections In C#

It is better to return empty collections rather than null when writing methods. The reason being that any code calling your method then doesn’t need to explicitly handle a special null case. Returning an empty collection makes the null check redundant and results in much cleaner method calling code. In C#, the System.Linq.Enumerable class has a useful generic method called Empty. When called with a type parameter, this method returns an empty instance of IEnumerable<T> (where T is your type parameter).
 
An example:
  1. public class House  
  2. {  
  3.    public IEnumerable<Person> CurrentOccupants { getset; }  
  4.    public House()  
  5.    {  
  6.       CurrentOccupants = Enumerable.Empty<Person>();  
  7.    }  
  8. }  
I also find that this method of instantiating (or returning) a collection states my intent more clearly than something like,
 
CurrentOccupants = new List<Person();
 
The MSDN documentation for Enumerable.Empty states that it caches the returned empty sequence of type TResult – so using this consistently for the same types can give (probably negligible) performance or memory usage advantages.
 
This pattern also reduces the chances of null reference errors from being raised in calling code. As mentioned above, the calling code then doesn’t need to go through the usual boiler-plate null check code on the returned collection. Naturally, this leads to cleaner code as the calling code can process the collection the same way if it has items or doesn’t have items within it.
 
Here you can learn more Collections in C#.