|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.
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:
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.
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:
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.
always works and so does
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."
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.
there is no problem and b is undefined. But, if you push your luck and write:
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.
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
or email your comment to: firstname.lastname@example.org
|Last Updated ( Monday, 03 March 2014 )|