What is the purpose of "?" in (someDelegateName)?.Invoke();? [duplicate]
Asked Answered
Z

2

13

The following code snippet was highlighted in Visual Studio alongside a suggestion for simplifying it.

if ( drawMethodsDelegate != null )
    drawMethodsDelegate ( e.Graphics );

When I clicked on the lightbulb suggestion thing, Visual Studio refactored it into the following

drawMethodsDelegate?.Invoke ( e.Graphics );

And no. The question mark is not a typo. I don't understand what the question mark is used for and I can't find anything relevant on MSDN. I also looked at the Tutorial Point Delegates page but found no useful information.

Tutorial Point page , MSDN Delegates page , MSDN Control.Invoke page

Zetta answered 5/3, 2017 at 11:45 Comment(2)
Search :c# null conditional operatorFeatherstitch
It's the C# 6 feature: Null-Conditional OperatorDandelion
M
24

This is the null conditional operator.

drawMethodsDelegate?.Invoke ( e.Graphics );

Provided that drawMethodsDelegate is not null calls the Invoke method. It is an operator that being introduced in the 6th version of C# and you can see it as a syntactic sugar, which helps you to write less code for handling null checks.

Last but not least, the above check is also thread-safe !

For further info please have a look here

Mealtime answered 5/3, 2017 at 11:47 Comment(13)
I like your answer as it provides more than the bare minimum of information. I don't understand why the question is getting downvoted thoughZetta
@JamesYeoman Thank you ! Unfortunately, in our community we have behaviors like these, downovotes without comments. I don't think that your question should be downvoted. Actually I upvoted it before started writing this comment. I hope some day all the donwvotes should be accompanied with a reasoning.Mealtime
The downvoting is likely because the answer is pretty easy to google and if ou hover over the downvote arrow it says "This question doesn't show any research effort"Unfathomable
@Unfathomable You have a point, about the googling. However I strongly believe in this case you should vote for closing the question and let a comment. Downvoting without a comment I don't think it is a good practice.Mealtime
@Mealtime To be honest, in any communtiy there are less intelligible people. For example, Weeabos in the Otaku culture/communtiy. Anyway, 3 more minutes until I can mark the accepted answerZetta
@Mealtime Downvoting without comment is a fundmental right of people on here and that should never be taken away.Unfathomable
@Unfathomable there is evidence for research effort though in the links to the websites that I tried...Zetta
@JamesYeoman it isn't a matter of intelligence. It is matter of respect. My personal opitnion it that a downvote without a comment is disrespectful. It doesn't help anyone.Mealtime
@JamesYeoman But had you Googled it, you likely would have found the answer.Unfathomable
@Unfathomable I didn't even know about the null conditional operator until the answers for the question started coming in.Zetta
@JamesYeoman Of course you dont know the name of a feature, but go search for "c# question mark" for example, I bet you one of the top links answers the question.Unfathomable
@Unfathomable That sort of search didn't occur to me at the time... I thought it was probably a delegate only thing as I had never seen it even suggested before... btw, I'm not trying to be pedantic or anything but the useful link was the third result. The first two were about conditional lambdas (or whatever the technical term for them are) which makes sense because they are more commonly seenZetta
@JamesYeoman Well I think the third link can be classified as "one of the top links". That's exctly what I've closed this question as a duplicate of.Unfathomable
K
3

This is a null condition operator that came with C# 6.0

https://msdn.microsoft.com/en-us/library/dn986595.aspx

it means IF drawMethodsDelegate is not null Invoke the method else do nothing.

Kamchatka answered 5/3, 2017 at 11:48 Comment(2)
It's a language feature (C# 6.0), not a .NET feature (4.6). C# 4.6 does not exists.Dandelion
You're right. My apologies.Kamchatka

© 2022 - 2024 — McMap. All rights reserved.