Monday, June 13, 2011

Best way to handle null in C# - Use Null Coalescing Operator

The Best way to handle null in C# - Use Null Coalescing Operator. You have seen some samples where I have used ?? operator, that is nothing but a "Null Coalescing Operator"

Null Coalescing Operator or ?? Operator in C# is supported in Visual Studio 2005, 2008, and 2010.

The Null Coalescing Operator or ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

string strData;
strData = rsDB("FieldName").ToString() ?? "No Data";

strData will get value "No Data" in case of rsDB("FieldName") is null.

Enjoy Green coding @ www.Code4Green.com