Bonus chatter: My favourite proposed feature for C# v6

Having been away on holiday recently, I’ve been doing a bit of catching up on the backlog of RSS-based reading that a week out of the office has generated. And one item stuck out at me as particularly interesting: some of the new features being discussed for the v6 release of the C# compiler. I came across a couple of articles discussing presentations given by Mads Torgersen (the C# Programme Manager) at the end of last year discussing some interesting feature proposals

Hey reader!
After many years, I’ve finally decided to start moving off WordPress and onto a blog I host myself. This post is now available at ““. Eventually this version will be retired so please update links on any sites you control, to avoid 404s in the future.

One thing stuck out at me as being particularly useful to the code I find myself dealing with in Sitecore solutions – something described as “Monadic Null Checking”. A rather technical name for a simple but useful syntax improvement.

When you’re using Model classes for your Sitecore templates, via a mapping tool like Glass, you often find yourself creating models that might look a bit like:

public class ExampleModel
    public virtual string Title { get; set; }
    public virtual Image SummaryImage { get; set; }

The mapping framework can fill this class in using the data from Sitecore Items – but what happens when that Image field defined here is not filled in by your content editors? Up until now you’ve generally ended up writing code that’s looked a bit like this:

public void DoSomething(ExampleModel model)
    if (model.SummaryImage != null)
        // do stuff with the data. For example:
        myImageControl.AlternatText = model.SummaryImage.Alt;

You end up littering your code with if() clauses to make sure that the properties of the object are not null before you make use of them. And it’s a common QA issue to find that when you test new code with the sort of edge-case data which content editors sometimes create, you get Null Reference exceptions in places where these guard clauses have been forgotten.

But in the future this could be simpler with a new approach to null-checking. The new “?.” operator is translated by the compiler into exactly the sort of null test we’ve been writing above. So our code becomes something like:

public void DoSomething(ExampleModel model)
    // do stuff with the data. For example:
    myImageControl.AlternatText = model.SummaryImage?.Alt;

If the value of SummaryImage is null, then the compiler never references the Alt child property – hence preventing the Null Reference issue without the extra code.

While you still have to remember to use this alternative syntax, it’s much shorter and (to me at least) the shorter code is easier to read and understand. Especially where you get into more complex scenarios with nested objects. And it’s much less hassle to remember to type “?.” every time you access a property than it is to write the whole guard.

4 thoughts on “Bonus chatter: My favourite proposed feature for C# v6

  1. Note that your two samples don’t do the same thing – you need something more like:
    myImageControl.AlternatText = model.SummaryImage?.Alt ?? myImageControl.AlternatText;

    and hope the compiler optimizes the assignment away.

    • Also, while I can’t tell, assuming the type of AlternatText is string, your code shouldn’t compile since the type of model.SummaryImage?.Alt is string?

      • Do you mean they work differently because the first one only overwrites the property when the new data is not null, but the second one always overwrites? If so, I see what you mean – but in the scenario I was thinking about I don’t think that’s a problem. Though I appreciate it could be a gotcha in other circumstances.

        Not sure I understand the second comment though. Did you mean that ?. changes the type of the expression? If so, I clearly have some more reading to do on this subject…

  2. Pingback: Another nice feature in C#6.0 | Jeremy Davis

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.