11 May 2007

Understanding C#: ?? Operator

No Comments Uncategorized

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

int? i = null;
int count = i ?? 0;

The value that count is set to is 0. The ?? operator is short hand for:

int? i = null;
int count = i.HasValue ? i.Value : 0;

Or

int? i = null;
int count = 0;
if (i.HasValue)
	count = i.Value;

The Understanding C# series at Coder Journal will be an on going project to help the readers to better understand the C# programming language that doesn’t get covered except at the more advanced levels.

Tags: , ,
written by
Nick Berardi
subscribe
If you found this post valuable and would like to see more like it you can follow me.
No Responses to “Understanding C#: ?? Operator”

Leave a Reply