Skip to content

Instantly share code, notes, and snippets.

@Superbil
Created February 24, 2011 19:01
Show Gist options
  • Save Superbil/842666 to your computer and use it in GitHub Desktop.
Save Superbil/842666 to your computer and use it in GitHub Desktop.
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