Monday, 3 March 2014

Safe Navigation Operator being considered for c#

One of the highest suggested features on UserVoice is the safe navigation operator. Now Mads Torgersen, PM of the C# Language, has expressed that they are considering implementing the feature. The next couple of months they will do some prototyping.

Why is this good and what is the Safe Navigation Operator?


Well, having a short hand null reference check would lighten up your code. Every time you're checking for null references in your code would get a lot easier and shorter. Lets have a look at it.

Lets say you're going to access an descendant of a given class. Our call would be Class.ChildClass.Property. Lets say for instance that ChildClass is null. We would have to check for a null reference of the Class type and ChildClass to be safe.


With the Safe Navigation Operator we would be able to write Class?.ChildClass?.Property without having to check for null references all the time.


Quite neat! If this is ever going to come to c# let's hope it's the next release. You can read more about the suggestion here.

/JJ


No comments:

Post a Comment