C # 4: how to define a string for zeros?

There was no short cut in C # 4 to check for null values:

if( myobject?.myproperty?.myotherproperty?.value != null ) 

The value returns null and does not throw an exception.

Does anyone have a link to how to use it, or at least the syntax?

+6
source share
5 answers

This operator is called Groovy's safe navigation operator .

It is not yet available in C #, even in C # 4.

If enough people demonstrate their support, perhaps it will fall into a hypothetical future version of C # ...

+9
source share

No, sorry, there is no such thing. They really considered it, but did not make a cut .

+4
source share

As others have said, in C # there is no built-in way to do this. A few months ago, I wrote a post about a way to do this using expressions using the NullSafeEval extension NullSafeEval :

 if (myobject.NullSafeEval(o => o.myproperty.myotherproperty.value) != null) ... 

But this is just a proof of concept, I have not tested it completely, and it is pretty slow ...

+2
source share

In C # there too? statement that is used to test against null. Is this a little better? Operator.

(x ?? -1) is equivalent to (x! = null? x: -1)

+2
source share

In C # / C ++, I can achieve this using the ternary operator, although the code will be disgusting. Are you sure you want to use this?

if ((myobject! = null? (myobject.myproperty! = null? (myobject.myproperty.myotherproperty! = null? myobject.myproperty.myotherproperty.value: null): null): null)! = zero)

 class MyOtherProperty { public string value; } class MyProperty { public MyOtherProperty myotherproperty; } class MyObject { public MyProperty myproperty; } 

My Unit test code:

  [TestMethod()] public void TestTernaryOperator() { MyObject myobject = new MyObject(); Debug.WriteLine (string.Format ("{0}, {1}", myobject != null, myobject.myproperty != null)); Debug.WriteLine(string.Format("IsNotNull = {0}", IsNotNull(myobject))); myobject.myproperty = new MyProperty(); Debug.WriteLine (string.Format ("{0}, {1}, {2}", myobject != null, myobject.myproperty != null, myobject.myproperty.myotherproperty != null)); Debug.WriteLine(string.Format("IsNotNull = {0}", IsNotNull(myobject))); myobject.myproperty.myotherproperty = new MyOtherProperty (); Debug.WriteLine (string.Format ("{0}, {1}, {2}, {3}", myobject != null, myobject.myproperty != null, myobject.myproperty.myotherproperty != null, myobject.myproperty.myotherproperty.value != null)); Debug.WriteLine(string.Format("IsNotNull = {0}", IsNotNull(myobject))); myobject.myproperty.myotherproperty.value = "Hello world"; Debug.WriteLine(string.Format("{0}, {1}, {2}, {3}", myobject != null, myobject.myproperty != null, myobject.myproperty.myotherproperty != null, myobject.myproperty.myotherproperty.value != null)); Debug.WriteLine(string.Format("IsNotNull = {0}", IsNotNull(myobject))); } bool IsNotNull(MyObject myobject) { bool isNotNull = (myobject != null ? (myobject.myproperty != null ? (myobject.myproperty.myotherproperty != null ? myobject.myproperty.myotherproperty.value : null) : null) : null) != null; return isNotNull; } 
+1
source share

All Articles