Is there a coalesce operator for property properties in C #?

So, is there a coalescing operator ?? , which allows you to conveniently handle null objects (IE. MyDisplayString = MyString ?? "n/a"; )

but is there a nice fancy operator to handle a similar situation on the properties of objects? For example, suppose a property that interests you is a property of a property of the type: MyDataObject.MySubModel.MyProperty

If MyProperty is null, you want to combine it into "n / a". Can you use ?? here, but what if MyDataObject is null or MyDataObject.MySubModel ?

This is also related to XML when trying to get optional attributes and elements of an element. IE: MyString = MyElement.Attribute("MyOptionalAttribute").Value ?? "n/a"; MyString = MyElement.Attribute("MyOptionalAttribute").Value ?? "n/a"; fails if the attribute is missing.

Is there a nice way to handle this scenario?

+6
c #
source share
6 answers

Is there an attractive way to handle this scenario?

You are not the first to offer this feature . One way is to write a β€œWith” extension method to retrieve property values, because extension methods can handle a call by a null reference. Instead

 thing.Foo.Bar 

you write

 thing.With(x => x.Foo).With(x => x.Bar) 
+6
source share

In C # 5 and below, as indicated in other answers, you need to create something yourself. Instead of using the Extension method, like others, an assistant is used here, which we call NN, since we use its 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 several helpers depending on the desired behavior when zero occurs ; for example, you can access the int property and want 0. Full code in the associated Gist.

In C # 6, you can use the null property union operator:

 myObjThatCouldBeNull?.ChildPropIWant 

More on this:

https://roslyn.codeplex.com/discussions/540883

+3
source share

Zero Object Template

A null object is an object with (neutral) behavior

can help you avoid this problem.

Another option is to use extension methods , then you can say:

 if (Contract .NullSafe(c => c.Parties) .NullSafe(p => p.Client) .NullSafe(c => c.Address) != null) { // do something with the adress Console.Writeline(Contract.Parties.Client.Adress.Street); } 
+2
source share

As I noted here:

Label for "null if the object is null, or object.member if the object is not null"

this is a rather frequently requested function that did not create a bar for C # 4. We will consider it for hypothetical future versions of the language, but it is not too high in the list of priorities, so I won’t hope much if I were you.

+2
source share

It may be a better or more elegant way to handle the problem you are describing, but I found that I usually have to check for null values ​​in the path, i.e.:

 MyString = MyElement.Attribute("MyOptionalAttribute") != null ? MyElement.Attribute("MyOptionalAttribute").Value : "n/a"; 
0
source share

In the case of XElement / XAttribute, you can use the explicit conversion operators :

 string myString = (string)myElement.Attribute("MyOptionalAttribute") ?? "n/a"; 

In general, the "safe dereference operator" is a fairly frequently requested function .

0
source share

All Articles