I am facing inconsistent null value reference errors while I am trying to store my values from a serialized class object.
if ( item.current_location.city!=null )
{
var city = item.current_location.city.Select(i => i.ToString());
}
In the above code snippet, successful insertion takes place even if any index in item array has null values. But it throws exception in some cases,which I don’t think can be distinguished in any manner from other cases( when the value is null)
itemcould be null as wellcurrent_locationcould be null as well,not only
city.This would help
EDIT:
Note: This code works, since c# implements a so-called shortcut-evaluation of Boolean expressions. If
itemshould benull, the rest of the expression would not be evaluated. Ifitem.current_locationshould benull, the last term would not be evaluated.(I do not see any insertion in the code above.)
Starting with C#6.0 you can use the null propagation operator (?):