tags:

views:

149

answers:

5

In C# 4, wasn't there a short cut for checking for null values like so:

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

The value would return null and not throw an exception.

Anyone have a link to how to use it or at least the syntax?

+4  A: 

Nope, sorry, no such thing. They did consider it, but it didn't make the cut.

Joren
+9  A: 

This operator is called the safe navigation operator in Groovy.

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

If enough people show their support for it, maybe it will get into a hypothetical future version of C#...

Mark Byers
+1  A: 

In C#/C++, I can achieve this using the ternary operator although the code would be hideous. 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) != null)

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;
    }
Syd
I am an old unix and c guy, so I actually prefer this approach over the if( this == null) then if (thisOtherThing == null) etc.
Dr. Zim
+2  A: 

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

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

But it's just a proof of concept, I didn't test it thoroughly, and it's pretty slow...

Thomas Levesque
+2  A: 

In C# there's also ?? operator that is used for testing against null. This is slightly better than ? operator.

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

kubal5003
also known as the "Null coalescing operator". one of the seldom used keywords.
Syd