Tuesday, March 16, 2010

Using the ?? Operator

All the variables that contain null are displayed as blanks. We can change this by assigning a default value to the nullable variable. Use the ?? operator to assign a default value that will be applied when a variable with the value null is assigned to another variable. For example:
int? myInt = null;
int yourInt = myInt ?? -5;
The first statement assigns null to myInt, and the second statement assigns the default value(-5) to myInt, and then assigns it to yourInt. Now when we display yourInt we get -5.

No comments:

Post a Comment