Created
January 18, 2014 16:05
-
-
Save ciarancolgan/8492424 to your computer and use it in GitHub Desktop.
Trying to call the 'Value' property of Nullable parameters vs GetValueOrDefault
This file contains 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
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