May 11th, 2007

Understanding C#: ?? Operator

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: ,

Social: kick it on DotNetKicks.com | Bookmark | View blog reactions

This entry was posted on Friday, May 11th, 2007 at 2:06 pm and is filed under C#, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply