Wednesday, July 16, 2008 3:14 PM
I enjoy the new C# language features very much. Recently I started using the new ?? operator in my current projects. The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. It is specially handy when you need to return a default value from your custom methods. A good example would be you wanted to capture the query string values in a web page but you needed to avoid the null reference exception in case the query string doesn't exist in the request. Consider the following code.
1: private string GetQueryStringValue(string name, string defaultValue)
2: {
3: return Request[name] ?? defaultValue;
4: }
If the query string doesn't exist, the string defaultValue will be returned.