Avatar ·

What do two question marks in a row mean in C#?

Hello! I just started learning C# and came across some strange syntax. In someone else's code, I saw this construct:\n\n```csharp\nstring name = userName ?? "Guest";\n```\n\nWhat do these two question marks `??` do here? I understand that `userName` is some variable, but why put two questions and then a string? Is it some kind of check? I tried googling, but I mostly find explanations about the ternary operator `? :`, and this looks different.\n\nI also noticed that sometimes they write `??=` — is that related too? For example, in this code:\n\n```csharp\nlist ??= new List();\n```\n\nCan you explain in simple terms how this works and why it's needed? Preferably with examples, because I'm a visual learner. Thanks in advance!\n<<>>\n

Null-coalescing operator (??) in C#

\n

\nTwo quest