Wednesday, March 11, 2009

C#- The Null Coalescing Operator

The null coalescing operator is a new operator introduced as a part of the .NET framework version 2.0
The operator's symbol is a double question mark (??).

The null coalescing operator tests the value of a variable to check if it is null. If the value is not null then the variable's value is returned unaffected. If the variable is null however, a substitute value is provided as a second operand.

nt? a = 10;
int? n = null;
int result;

result = a ?? -1; // result = 10
result = n ?? -1; // result = -1

1 comment: