mostlylucid

scott galloway's personal blog...
posts - 911, comments - 723, trackbacks - 11

My Links

News

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 ]

Comments have been closed on this topic.

Powered by: