What does "??" do in C#?

.net, c#

Solution

A ?? B

is a shorthand for

if (A == null) 
    B
else 
    A

or more precisely

A == null ? B : A

so in the most verbose expansion, your code is equivalent to:

MemoryStream st;
if(stream == null)
    st = new MemoryStream();
else
    st = stream;

Problem

I found this new and interesting code in my project. What does it do, and how does it work? ``` MemoryStream stream = null; MemoryStream st = stream ?? new MemoryStream(); ```

Original source

Related problems