7/31/2010
LifeCycle Solutions - Home ( the software development blog )
 

<January 2007>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Subscribe to this feed:

RSS 2.0 | Atom 1.0 |CDF






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