mostlylucid

STATIC ARCHIVE of mostlylucid.co.uk of old
posts - 892, comments - 676, trackbacks - 11

My Links

News

follow me on twitter!

Archives

Post Categories

Misc. Coding

C# null Coalescing operator

Just because I always forget this...the C# null Coalescing operator:

Sample below shamelessly stolen from here...

MyClass anObject;
...
// Variant 1: Using a full if/else clause
MyClass anotherObject;
if (anObject != null)
  anotherObject = anObject;
else
  anotherObject = new MyClass();
 
// Variant 2: Using the ?/: conditional operator
MyClass anotherObject = anObject != null ? anObject : new MyClass();
 
// Variant 3: Using the ?? operator
MyClass anotherObject = anObject ?? new MyClass();

Print | posted on Saturday, November 03, 2007 5:07 PM | Filed Under [ .NET Code Snippets ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 4 and type the answer here:

Powered by: