Created
February 24, 2011 19:01
-
-
Save Superbil/842666 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Test | |
{ | |
class NullTest | |
{ | |
static int? GetNullableInt() | |
{ | |
return null; | |
} | |
static string GetStringValue() | |
{ | |
return null; | |
} | |
static void Main() | |
{ | |
// ?? operator example. | |
int? x = null; | |
// y = x, unless x is null, in which case y = -1. | |
int y = x ?? -1; | |
Console.WriteLine(y); | |
// Assign i to return value of method, unless | |
// return value is null, in which case assign | |
// default value of int to i. | |
int i = GetNullableInt() ?? default(int); | |
Console.WriteLine(i); | |
string s = GetStringValue(); | |
// ?? also works with reference types. | |
// Display contents of s, unless s is null, | |
// in which case display "Unspecified". | |
Console.WriteLine(s ?? "Unspecified"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment