When I use a method that return an IEumerable
, First i check it for Null value and then check it for, Is there any Item.
public IEnumerable<Order> GetOrdersByServiceName(string serviceName)
{
if (IsValidService(serviceName))
return null;
var orders = GetValidOrder(serviceName);
return orders;
}
Usage:
var orders=GetOrdersByServiceName("TestService");
if(orders == null or !orders.any())
//do something
If GetOrdersByServiceName
return Null and I forget Null checking, Code will throw Null Reference Exception at some condition.I want eliminate orders == null
and just use !orders.any()
in If
statement.
I think two condition has same semantic and tell me result is nothing (Null Object pattern):
- !orders.any()
- orders == null
To realize that all Developer at my team must make sure every method which return IEnumerable , return `Enumerable.Empty() instead of Null value.
My Question Is: Are there any scenario that result of called method -Null or Empty Array- have different semantic for caller method?