Although I would say that this is mainly a matter of developer preference, returning an empty collection may be the best approach.
Say you have an object that contains a collection item.
public class Customer { private IList<Order> _orders; public Customer() { _orders = new List<Order>(); } public IList<Order> Orders { get { return _orders; } } }
As a rule, this participant will prefer as a readonly property that his customer orders are not lost without any apparent reason. So returning null not an option. You are probably working better with an empty collection than with a null reference. So instantiating in a class constructor is a better approach.
And most importantly, for example, when using DataBinding, you may get strange behavior when returning a reference to a null collection, as this will work best with an empty collection.
Another example when iterating through a collection, for example:
foreach (Order o in c.Orders) {
This foreach simply not execute when the collection is empty, without first having to check if it is a null reference. This simplifies the code and minimizes its complexity.
It depends on the scenario in which you are aiming.
source share