13

I've seen code like the following unrelated lines:

 Console.Write(myObject?.ToString());
 return isTrue ? "Valid" : "Lie";
 return myObject ?? yourObject;
 int? universalAnswer = 42;

There seems to be more in C#8+ like

 public static Delegate? Combine(params Delegate?[]? delegates)...
 string? value = "bob";

Are all of the usages of the question mark related or different? What do each of them mean?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • @DavidG sort of... Turned out that there is no searchable answer for http://stackoverflow.com/questions/43074622/question-mark-after-session-variable-reference-what-does-that-mean?noredirect=1. I'm not sure if closing as duplicate of 4 is right solution - will see in several days how search engines deal with it. – Alexei Levenkov Mar 28 '17 at 16:37
  • @DavidG good point - I did not notice if self-answer interface has CW option or not - changed to CW. – Alexei Levenkov Mar 28 '17 at 16:40
  • Long time ago there was an option to mark question/answer as community wiki. Like for `NullReferenceException` topic. Can't find this option now.. – Sergey Berezovskiy Mar 28 '17 at 16:40
  • I wonder if posts should be duped to this one though? – DavidG Mar 28 '17 at 16:49
  • 1
    @SergeyBerezovskiy Only answers can be marked as CW and only by the answerer or a mod. [Questions can't be done by anyone but a mod](https://meta.stackexchange.com/a/67192/261748) – DavidG Mar 28 '17 at 16:53
  • @DavidG I've asked this about it on Meta - https://meta.stackoverflow.com/questions/346099/summary-self-answer-close-as-multiple-dup-or-links. – Alexei Levenkov Mar 28 '17 at 17:01
  • if you guys are going to mark a question as a dup, can you provide a link to that dup? – Gerry Nov 02 '17 at 15:14
  • @Gerry as links in the answer and links on top of the question not enough for you where do you expect to see those links also? – Alexei Levenkov Nov 02 '17 at 17:00

1 Answers1

44

Question marks have different meaning in C# depending on the context.

The Null-Conditional Operator (MSDN, What does the question mark in member access mean in C#?)

Console.Write(myObject?.Items?[0].ToString());

The Conditional Operator/Ternary Operator (MSDN, Benefits of using the conditional ?: (ternary) operator)

return isTrue ? "Valid" : "Lie";

The Null Coalescing Operator (MSDN, What do two question marks together mean in C#?)

return myObject ?? yourObject;

Nullable Value Types (MSDN, What is the purpose of a question mark after a type (for example: int? myVariable)?)

int? universalAnswer = 42;

Nullable Reference Types C# 8 added nullable reference types with many more options to sprinkle question marks (it also gave new place to put explanation marks - null!" is a null-forgiving operator):

Nullable string (remember that string is reference type behaving like value type):

string? value = "bob"; 

Nullable array of nullable reference objects - What is the ?[]? syntax in C#?

public static Delegate? Combine(Delegate?[]? delegates)
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • May I ask why would someone set string as `Nullable` if it can contain(?) `null` by default? Is it because of `Nullable` perks/features like `.HasValue`, for example? – Artfaith Oct 21 '20 at 18:55