Search

A bit more C# syntactic sugar for nulls by Miral

Closed
as Won't Fix Help for as Won't Fix

52
3
Sign in
to vote
Type: Suggestion
ID: 192177
Opened: 9/1/2006 11:35:04 PM
Access Restriction: Public
1
Workaround(s)
The addition of the ?? operator in C# 2.0 is quite handy, but I still find myself repeating one particular null pattern quite a lot:

return ((obj == null) ? null : obj.propertyOrMethod);

It'd be nice if there was some form of the '.' operator that meant "call or access this member if the instance isn't null, otherwise just return null". Maybe "?." as in "return obj?.propertyOrMethod;"..? That's sort of along similar lines as "??".
Details (expand)
Product Language
English
Version
Visual C# 2005 Express Edition
Operating System
Windows XP Professional
Operating System Language
English
Category
\Compilers
Subcategory 1
\C#
Subcategory 2
 
Subcategory 3
 
Problem Statement
No problem; just a suggestion.
Proposed Solution
Essentially "(a?.b)" would be exactly equivalent to "((a == null) ? null : a.b)".

Just like "." itself, you should be able to chain a whole pile of them together, eg "obj1?.prop1?.prop2?.prop3" (where any of the intermediate properties or the original object could be null, in which case the whole expression returns null), or "obj1?.prop1.prop2?.prop3" (where you either know that prop1 can't be null, or you want to throw a NullReferenceException if it is).

It's fairly clear-cut for properties. It's a little less so for methods; ideally though it'd work for them as well, in a short-circuit fashion. ie. if the LHS is null then the method doesn't get called, and the call expression is considered to return null (if the method returns a reference or nullable type), or do nothing (if the method is void). If the method returns a value type, I'm not sure what correct behaviour would be there. Maybe that should be a compiler error, or maybe it should return the default value (eg. 0 for integers).
Benefits
Faster Development
Sign in to post a comment.
Posted by indomitable on 7/1/2011 at 7:05 AM
Hi Microsoft.
This feature was not implemented in C# 3 neither in C# 4, but what about C# 5 ?
Java 7 is comming and has it, what about C# ?
Posted by JoeGtp on 12/11/2008 at 1:21 PM
Groovy has a operator like this http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29 and it is very useful indead.
Posted by JoeGtp on 12/11/2008 at 1:20 PM
Groovy has a operator like this http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29 and it is very useful indead.
Posted by Microsoft on 4/24/2008 at 4:36 PM
Thanks again for your suggestion. After having done feature planning for the next release of C# I regret to say that this feature is not being added. We have to do some harsh prioritization, both because of our implementation and testing resources, but also because we need to keep the number of new langauge features at a manageable level - depending on how you count, we are adding only four language features to C# this time around. Unfortunately many great suggestions just can't make it in because of that.

I apologize that this is a "canned" follow-up answer, sent out as a result of our feature planning for the next release. In most cases I or someone else already replied individually to your suggestion - please let us know if you feel it hasn't been adequately addressed.

Thanks again for taking the time to share your ideas with us. Please keep them coming!

Mads Torgersen, C# Language PM.
Posted by Microsoft on 10/16/2007 at 12:55 PM
Thanks for your proposal.

This is an issue that keeps coming up, and we discussed it again on the C# design team after seeing your suggestion. This is a fairly common nuisance, yet introducing a new kind of "dot" operator is not something to be done lightly.

We're on the fence with this issue, and keeping it open for our discussions around what should end up in the next release (after C# 3.0, which closed for new features some time ago).

Thanks again,

Mads Torgersen, C# Language PM
Posted by Miral on 9/15/2006 at 6:23 PM
(To IDisposable and anyone else who hadn't noticed, there's more discussion down in the 'Proposed Solution' section, including syntaxes for chains of these, plus what to do when value types are involved.)
Sign in to post a workaround.
Posted by Ed Ball on 1/14/2008 at 8:37 AM
The best workaround we could come up with was this extension method:

http://code.logos.com/blog/2008/01/nullpropagating_extension_meth.html