kalki
kalki

Reputation: 526

Lambda Expressions

Is there a better way to avoid NullRefenrenceException other than try/catch

i.e

x => x.Child.GChild.GChildProp1

If Child Or GChild happen to be null obviously this would raise NullRefenrenceException

other solution that popped into the head, is to evaluate each expression part individually from left ?

which would be better or it there a better solution ?

Upvotes: 1

Views: 330

Answers (4)

Guffa
Guffa

Reputation: 700690

If you want a single expression, you could use a generic method that can handle null references without causing an exception:

public static R TryGet<T,R>(T obj, Func<T,R> getProperty) {
  if (obj == null) {
    return default(R);
  } else {
    return getProperty(obj);
  }
}

Now you can use that to try to get a property:

x => TryGet(TryGet(TryGet(x, y => y.Child), y => y.GChild), y => y.GChildProp1);

If you get a null reference on the way, you end up with the default value of the final property (which is null if it's a reference type).

Upvotes: 0

Christian
Christian

Reputation: 7852

Exceptions should only be thrown in exceptional cases i.e. in cases you can't possibly account for (i.e. out of memory etc.). Using try/catch for cases like above is considered bad practice (for performance and degbuggability reason etc) and can be avoided by checking for null references i.e.

x => { if (x.Child != null && x.Child.GChild != null) x.Child.GChild.GChildProp1; };

Best regards, Christian

Upvotes: 0

slugster
slugster

Reputation: 49985

Hmmm, is there anything fundamentally wrong with this?

myList.Where(x => x.Child != null && x.Child.GChild != null).Select(x => x.Child.GChild.GChildProp1);

or:

var z = from x in myList
        where x.Child != null && x.Child.GChild != null
        select x.Child.GChild.GChildProp1;

Note there was no null check on the third property in the heirarchy as i presume it is okay to select those that are null.

I'm mainly throwing this out there because Jon's answer interests me and i want him to explain more about changing this to an expression tree - mainly because i don't like popping out to a separate function in the middle of a linq expression unless i really have to (just personal preference, others may have a different opinion :)

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503090

Well, you could convert that into an expression tree instead of into a delegate, and carefully navigate it. But no, C# doesn't have anything like a "null-safe dereferencing operator". I gather the C# team has looked into it, and found it hard to come up with something which actually works the way you'd naturally want it to. That's not to say they won't have another look at a later date, of course :)

(It certainly sounds like a relatively easy language feature to implement... but I trust the team when they say there are hidden complexities!)

I wouldn't really want to catch a NullReferenceException as a way round this though... I'd prefer to write a method which checked for null references explicitly for the particular case. It depends on your situation though.

Upvotes: 2

Related Questions