Tuesday, 15 May 2012

c# - Converting to Nullable Type without extra method -



c# - Converting to Nullable Type without extra method -

is writing kosher? or there problems may arise?

private datetime? getdate(object date) { homecoming date != null ? convert.todatetime(date) : (datetime?)null; }

i've seen lot of questions asking similar question, reply given extension method job of converting, wondering if skip step , write this, or there border case not accounting for?

also i'm using datetime in example, think work nullable type.

since ? type of nullable<t>, fine , 'kosher' long aware of actual homecoming type. if writing method as:

private nullable<datetime> getdate(object date) { ... }

as long aware below won't work because homecoming types differ:

datetime mydatetimevariable = getdate(someobject); because it's type conversion problem.

you can see explaination: nullable datetime?

c# type-conversion nullable

No comments:

Post a Comment