Friday, August 6, 2010

Double Question mark / coalesce operator

DOUBLE QUESTION MARK (this operator is named the coalesce operator) ??

The double question mark operator returns the first value that is not null. This was a feature introduced in .NET 2.0 ( mm..However, I never wrote something like this earlier)

Example 1:-

(intVal1 ?? intVal2) ?? 100 => retuns 100 if intVal1 and intVal2 is null
(intVal1 ?? intVal2) ?? 100 => retuns intVal1 if intVal2 is null and intVal1 is not null
(intVal1 ?? intVal2) ?? 100 => retuns intVal2 if intVal1 is null and intVal2 is not null

Example 2 :-

x = y ?? z; --> If y is null x will be set to z.

No comments: