Using the Null-Coalescing Operator for Instantiation

27 06 2011

The null-coalescing operator is used to define a default value for reference types and nullable value types. It takes two operands, returning the left operatand if it is not null, otherwise it returns the right operand. I find it very useful when combined with properties to instantiate variables which can be null.

public class Book
   private String m_Title;

   public String Title
          return (m_Title = m_Title ?? String.Empty);



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: