c# - Convert decimal? to double? -
i wondering best way (in sense of safer , succinct) convert 1 nullable type "compatible" nullable type.
specifically, converting decimal? double? can done using:
public double? converttonullabledouble(decimal? source) { return source.hasvalue ? convert.todouble(source) : (double?) null; }
is there better way this? maybe leveraging standard conversion?
built in casts win! tested in vs2012 and vs2010:
decimal? numberdecimal = new decimal(5); decimal? nulldecimal = null; double? numberdouble = (double?)numberdecimal; // = 5.0 double? nulldouble = (double?)nulldecimal; // = null
just using explicit cast cast null null, , internal decimal value double. success!
Comments
Post a Comment