So there is the coalescing operator ?? that allows handy handling of null objects (IE. MyDisplayString = MyString ?? "n/a";)
but is there a nice fancy operator for handling a similar situation on properties of objects? For instance lets say that the property you are interested in is a property of a property like: MyDataObject.MySubModel.MyProperty
If MyProperty is null you want coalesce to "n/a". You can use ?? here, but what if MyDataObject is null or MyDataObject.MySubModel?
This also comes up with XML when trying to get optional attributes and elements of an element. IE: MyString = MyElement.Attribute("MyOptionalAttribute").Value ?? "n/a"; fails if the attribute isn't there.
Is there a nice fancy way of handling this scenario?
The COALESCE expression allows for user-specified NULL-handling. It is often used to fill in missing values in dirty data. It has a function-like syntax, but can take unlimited arguments, for example: COALESCE(a, b, c, x, y, z) .
The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null ; otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
In simplest way, two question marks are called "Coalescing Operator", which returns first non null value from the chain. e.g if you are getting a values from a nullable object, in a variable which is not nullable, then you can use this operator.
Null-coalescing Operator(??) Null-coalescing Operator is a binary operator that simplifies checking for null values. it is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise, it returns the right operand.
Is there a nice fancy way of handling this scenario?
You are not the first one asking for this feature. One way is to write a "With" extension method to fetch property values, since extension methods can handle being called on a null reference. Instead of
thing.Foo.Bar
you would write
thing.With(x => x.Foo).With(x => x.Bar)
In C# 5 and below as stated in other answers you need to build something yourself. Instead of using an Extension method as others have here we use a Helper, that we call NN since we use it a LOT, especially in Razor.
public static TValue N<TParent, TValue>(TParent o, Func<TParent, TValue> accessor, TValue defaultValue)
{
if (o == null)
return defaultValue;
return accessor(o);
}
/// <summary>
/// Guarantees return of null or value, given a prop you want to access, even if the parent in the first argument
/// is null (instead of throwing).
///
/// Usage:
///
/// NN.N(myObjThatCouldBeNull, o => o.ChildPropIWant)
/// </summary>
/// <returns>The value of the prop. Null if the object is null, or if the child prop is null.</returns>
public static TValue N<TParent, TValue>(TParent o, Func<TParent, TValue> accessor)
where TValue : class
{
return NN.N(o, accessor, null);
}
We actually use a few helpers depending on the desired behavior when null is encountered; for example you might be accessing an int property and want 0. Full code in the linked Gist.
In C# 6, you can use the null property coalescence operator:
myObjThatCouldBeNull?.ChildPropIWant
More about it:
https://roslyn.codeplex.com/discussions/540883
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With