tags

proof of concept

Automating null checks with Linq expressions

The problem Have you ever written code like the following ? X xx = GetX(); string name = "Default"; if (xx != null && xx.Foo != null && xx.Foo.Bar != null && xx.Foo.Bar.Baz != null) { name = xx.Foo.Bar.Baz.Name; } I bet you have ! You just need to get the value of xx.Foo.Bar.Baz.Name, but you have to test every intermediate object to ensure that it's not null. It can quickly become annoying if the property you need is nested in a deep object graph….