Geeks With Blogs
Frank Wang's inspirations on .NET
IEnumerable<Inspiration> inspirations = from i in DataContext.Inspirations where i.Sharable == true select i

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.

Posted on Wednesday, July 16, 2008 3:14 PM C# | Back to top


Comments on this post: Shorten your conditional statements with the ?? operator

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Frank Wang | Powered by: GeeksWithBlogs.net