8/28/2008
LifeCycle Solutions - Home ( the software development blog )
 

<August 2008>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

Subscribe to this feed:

RSS 2.0 | Atom 1.0 |CDF





Add to Technorati Favorites

Thursday, January 04, 2007

One of the reasons I prefer C# to VB.Net is for its brevity (not true in every case, but generally so).  Adding to the list of its syntax shortcuts, C# 2.0 introduces a new operator:  "??", dubbed the "null coalescing" operator.  For you SQLServer gurus out there, it is somewhat analagous to the COALESCE() function in Transact-SQL...it evaluates a single value and returns a second value if the first value is null (note, the type involved must be nullable).  

string
customerName=null;
string newCustomerName=null
;

// a long way of assigning newCustomerName
if (customerName == null
) {
   newCustomerName = "Empty"
; }
else
{
   newCustomerName = customerName;}

// a slightly shorter approach, using ?: "ternary operator"
newCustomerName = (customerName == null ? "Empty"
: customerName);

// much more concise, using the new ?? operator
newCustomerName = customerName ?? "Empty";

More information here
.

Posted by Brian Parks

© 2006 LifeCycle Solutions, LLC | All Rights Reserved