Home Dashboard Directory Help
Search

A bit more C# syntactic sugar for nulls by Miral


Status: 

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)
view

Description

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
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