Returning Empty Collection

If you ever wanted to return an empty collection instead of returning null, you can use Enumerable.Empty. The Enumerable.Empty creates an empty collection of correct type specified by the generic parameter. Here is an example of returning generic empty collection.

[DataObjectMethodAttribute(DataObjectMethodType.Select)]
       static public IEnumerable<Category> GetProductSubcategories(int? catid)
       {
           if (catid != null)
           {
               return GetProductSubcategories(catid.Value);
           }
           return Enumerable.Empty<Category>();
           //return new List<Category>() ;
       }

1 Comment

Comments have been disabled for this content.