Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a coalesce operator for properties of properties in c#?

Tags:

c#

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?

like image 935
Mr Bell Avatar asked Oct 05 '10 16:10

Mr Bell


People also ask

What is coalesce in C?

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) .

What is use of null coalesce operator?

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.

What does two question marks mean in C#?

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.

IS null operator C#?

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.


2 Answers

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)
like image 149
Wim Coenen Avatar answered Sep 28 '22 09:09

Wim Coenen


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

like image 25
Chris Moschini Avatar answered Sep 28 '22 09:09

Chris Moschini