c# - Differences Nullable values: .Value or not? -
this question has answer here:
i'm kinda new nullable. have datetime in c# want null, assign value in try-catch, , after check whether it's null or not.
code:
datetime? dt = null; try { dt = datetime.parseexact(stringdate, "yyyy-mm-dd hh:mm:ss", cultureinfo.invariantculture); } catch(formatexception) { console.writeline("the given string not valid , not converter datetime"); } // check if datetime not null // if it's not null, datetime
so, question:
which of 2 should use, why should use it, , differences between both:
if(dt != null) { dosomethingwithdatetime(dt); }
or:
if(dt.hasvalue) { dosomethingwithdatetime(dt.value); }
Comments
Post a Comment