C# 6 Preview: The Safe Navigation Operator (?.)

The safe navigation operator, first announced and described in this blog post, has been available since the first Visual Studio 14 CTP due to its popularity in Visual Studio User Voice..

The operator, written as ?. in code, has been referred to by many names: safe navigation, null-conditional, null propagation, and conditional access are the ones I’ve come across so far.

Just to demonstrate the concept in an extremely simple manner, consider the following code:

            List<string> list = null;
            int count = list.Count;

It’s pretty obvious that if you try to run the above code, you’re going to get a NullReferenceException, because you’re trying to access the Count property of an object that is null.

Now, the example above can seem dumb because it’s pretty obvious that the list is null, but sometimes objects can come from external sources or user input, and so you have to be careful how you handle them. In this case we can work defensively by adding a null check:

            List<string> list = null;
            if (list != null)
            {
                int count = list.Count;
            }

That’s okay. But it can get pretty messy when there is a chain of references involved. Consider a binary tree, for instance.

var something = binaryTree1.LeftChild.LeftChild.Name;

If we were to write defensive conditionals for this, you effectively need a null check for each access.

            List<string> list = null;
            int? count = list?.Count;

The use of the safe navigation operator involves putting a question mark before the dot operator, as shown above. If list happens to be null (which it is in this case), then the Count property is not accessed, and null is immediately returned. Since list?.Count may return either a number or null, the count variable must now be nullable.

The safe navigation operator pays off pretty nicely when you have more complex reference accesses:

var something = binaryTree1?.LeftChild?.LeftChild?.Name;

It also works pretty nicely with the null-coalescing operator.

            List<string> list = null;
            int count = list?.Count ?? 0;

We can even use it to guard access to array elements when the array is null:

            string[] names = null;
            int length = names?[1].Length ?? 0;

…or guard access to array elements which may themselves be null:

            string[] names = new string[] { "Steve", null, "John" };
            int length = names[1]?.Length ?? 0;

The safe navigation operator is quite convenient in properties which make use of object instances. Consider the following property:

        public string SessionId
        {
            get
            {
                if (this.Session != null)
                    return this.Session.Id;
                else
                    return null;
            }
        }

We can now simplify this to:

        public string SessionId
        {
            get
            {
                return this.Session?.Id;
            }
        }

It’s also convenient to use the safe navigation operator to raise an event only if some delegate is subscribed to it. Consider an INotifyPropertyChanged implementation:

        public event PropertyChangedEventHandler PropertyChanged;

        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

You can’t just stick in a ?. before the paranthesised argument list. So the recommended way to do this is by using the Invoke() method:

        public event PropertyChangedEventHandler PropertyChanged;

        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            handler?.Invoke(this, new PropertyChangedEventArgs(name));
        }

The safe navigation operator isn’t going to change our lives. But it’s one of those sweet syntactic sugar features that we’ll grow to love in time.

One thought on “C# 6 Preview: The Safe Navigation Operator (?.)”

Leave a Reply

Your email address will not be published. Required fields are marked *