Object Eval Extension

1 min Edit
"Every extension of knowledge arises from making the conscious the unconscious."
Friedrich Nietzsche,

How often do you litter your code with long if-statement tests to guard against accessing a null reference of a property’s parent like so:

if (Person != null && !string.IsNullOrEmpty(Person.FirstName)) {
    var firstName = Person.FirstName;

Surely we could come up with a more elegant method in C# to break out of this cumbersome pattern that adds unnecessary bloat for other humans to read your actual business logic.

I came across a great pattern that solves this exact problem with a lot of elegance by leveraging of .NET’s default keyword. So the above example, could simply be re-written as follows:

var firstName = Person.Eval(p => p.FirstName);

The secret is in creating a chainable extension method that tries to either evaluate a property or return its default value if it fails a null check.

Check out the following gist for the complete example. It is one of those code gems that I always end up introducing to any project that I work on.

Eval Extension Gist