Friday, March 28, 2008

Coalesce Operator (??)

Coalesce means to merge or unite things. When you put this operator between two variables, it will return the first one that is not null.

Example:
string name = firstName ?? lastName ?? middleName ?? string.Empty;
This if statement does the same thing:
string name = string.Empty; 
if (firstName != null)
      name = firstName; 
elseif (lastName != null)
     name = lastName; 
elseif (middleName!= null)
     name = middleName;
More Info: MSDN: ?? Operator
Thanks goes to my friend, Rich, for this info.