Tuesday, September 25, 2007

Using ?? in C#

Ever got a object cannot be null kind of exception?
Here's a little tip I picked up from Scott Gu's blog:

string s = null;

string myNewString = s ?? "The original string was null";


the ?? operator will automatically check the variable s to see if it's null, and if it is, assign the value on the right hand side of ?? to myNewString.

Wonderful :)

0 kommentarer: