10

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

James Yeoman
  • 605
  • 3
  • 7
  • 17

2 Answers2

22

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

Christos
  • 53,228
  • 8
  • 76
  • 108
  • 1
    I like your answer as it provides more than the bare minimum of information. I don't understand why the question is getting downvoted though – James Yeoman Mar 05 '17 at 11:52
  • 1
    @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. – Christos Mar 05 '17 at 11:54
  • 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" – DavidG Mar 05 '17 at 11:55
  • @DavidG 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. – Christos Mar 05 '17 at 11:57
  • @Christos 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 answer – James Yeoman Mar 05 '17 at 11:57
  • @Christos Downvoting without comment is a fundmental right of people on here and that should never be taken away. – DavidG Mar 05 '17 at 11:58
  • @DavidG there is evidence for research effort though in the links to the websites that I tried... – James Yeoman Mar 05 '17 at 11:58
  • @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. – Christos Mar 05 '17 at 11:59
  • @JamesYeoman But had you Googled it, you likely would have found the answer. – DavidG Mar 05 '17 at 11:59
  • @DavidG I didn't even know about the null conditional operator until the answers for the question started coming in. – James Yeoman Mar 05 '17 at 12:01
  • @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. – DavidG Mar 05 '17 at 12:02
  • @DavidG 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 seen – James Yeoman Mar 05 '17 at 12:06
  • @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. – DavidG Mar 05 '17 at 12:08
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.

Stralos
  • 4,895
  • 4
  • 22
  • 40