0 votes
by (2.5k points)

I've seen C# code written as : 

var myValue = someOtherValue ?? 10;

What does the double question marks mean? 

1 Answer

0 votes
by (2.5k points)

Double question marks in C# stand for the "Null Coalescing Operator" which sounds scary but really isn't. All it means is that "If this value is null, then use this other value". 

For example : 

int? nullableInt = null;
int myValue = nullableInt ?? 10;

In this example, myValue would be set to 10 as the variable nullableInt is null. 

However in this example :

int? nullableInt = 5;
int myValue = nullableInt ?? 10;

myValue would be set to 5 as the value is not null. 

It's extremely handy when you want to use a value OR a default if it happens to be null at that point in time. 

Welcome to .NET Q&A, where you can ask questions and receive answers from other members of the community.