Chaining the C# ?? Operator
Posted by: Rick Strahls WebLog,
on 24 Jan 2008 |
View original | Bookmarked: 0 time(s)
C# 2.0 has a nice ?? operator that works as a shortcut for: string value1 = null;string value2 = "Test1";string result = value1 != null ? value1 : value2;
which causes result containing Test1 or the second value.
In C# you can shortcut this special null comparison case with the new ??:
string result = value1 ?? value2;
which is a little easier to write. This is probably not news to you, but what's really useful is that you can chain these operators together so you can do a whole...