C# Gets A New Operator - Safe Navigation
Written by Ian Elliot   
Monday, 03 March 2014

One of the big problems with computer languages is they tend to throw their hands in the air (metaphorically) and give up at the slightest problem in executing your code. Now C# is taking a small step towards being just a little more forgiving and a little more determined in running your code. 

 

csharp

 

The problem that the new operator seeks to solve is what happens when you try and access something that doesn't exist.  For example if you write:

var result = object1.object2.object3;

then everything is fine as long as the object exist. If,say, object2 doesn't then you get a NullReferenceException.  

Throwing an exception is  a bit of an over reaction and the only way the programmer can prevent the exception is to test each object reference in turn before using one of its properties. The problem is that you can't ask for a property on a null object. 

If you think about it, however, all that has to change is for a reference to a null object's property to return null.

This is what the new ? operator is for. 

The expression

var result=object1?.object2?.object3;

doesn't throw an exception if object2 is null. Instead it returns null if any of the objects is null. This means that you can test for any problems with the expression simply by checking it result is null. Of course this doesn't mean you can know which object caused the problem but knowing that the final reference is null is often enough to determine what should happen next. 

Put simply, the expression:

object?.property

returns null if the object is null and object.property if the object is anything else. 

Similarly you can use the ?. operator with method invocation and indexing in the same way.

object?.method();

always works and so does

object?[index]

 

The new operator is something that hit the top of a Microsoft user poll with 3752 votes and its success is reported on an MSDN blog. where it is also reported that the operator might come to Visual Basic as well according to the C# Project Manager Mads Torgersen.

Another telling comment is:

"Another inherent implication of Mad’s comment is the confirmation of the next version of C#. Of course, I think we all knew there would be another version of C#, but we can’t be certain until there’s something publically announced."

With Anders Hejlsberg's attention seeming to be on TypeScript and a general increased emphasis by Microsoft on C++ and JavaScript, any news that C# is moving forward is good.

Is the safe navigation operator a good idea?

As long as it fits in with the existing language then it should be fine. In general languages give up too easily and throw unnecessary runtime exceptions.

Languages that don't do this tend to be thought of as easy to use and powerful enough for meta programming. Take PHP for example, not a great language but usually it will roll on and create some sort of web page to serve up to a user even if there is an error.

Ruby on the other hand has facilities for dealing with missing methods that makes it very easy to extend the language.

JavaScript, however, is a strange mix of "keep going" and "throw an exception". If you write:

var a;
var b=a;

there is no problem and b is undefined. But, if you push your luck and write:

var c=a.prop;

and a is undefined or null then you get a runtime exception. If a is an object that doesn't have prop then you get undefined for c, but if you try and call a method that a doesn't have you get a runtime exception. This is confusing and it would be better if all references were either evaluated or returned null.

Giving up and throwing an exception should be a last resort, not a first choice.

 csharp

 

Banner


A $39 Nao For Xmas!
29/11/2014

Before you get too excited it's an action figure, not the real robot. Aldebaran now has a shop for fans of the cutest robot ever made. 



Mechanical Insects Evolve The Ability To Fly Though A Window
07/12/2014

You might think that the world has enough insects without creating robots in the same style. In this case, however, the real interest is in the way the ability to fly though a window can evolve withou [ ... ]


More News

 

Last Updated ( Monday, 03 March 2014 )
 
 

   
RSS feed of news items only
I Programmer News
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.