Skip to content

Instantly share code, notes, and snippets.

@ciarancolgan
Created January 18, 2014 16:05
Show Gist options
  • Save ciarancolgan/8492424 to your computer and use it in GitHub Desktop.
Save ciarancolgan/8492424 to your computer and use it in GitHub Desktop.
Trying to call the 'Value' property of Nullable parameters vs GetValueOrDefault
namespace Test
{
class Program
{
static void Main(string[] args)
{
int? nullableInt = null;
int? nullableInt2 = 1;
SeeIfThisWorks(nullableInt, nullableInt2);
}
private static void SeeIfThisWorks(int? nullableInt, int? nullableInt2, int? notPassed = null)
{
var a = nullableInt.GetValueOrDefault(); //equal to 0
var b = nullableInt2.GetValueOrDefault();
var c = notPassed.GetValueOrDefault(); //equal to 0
var d = nullableInt.Value; //throws error
var e = nullableInt2.Value;
var f = notPassed.Value; //throws error
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment