I am wondering what would be the best way (in the sense of safer and succinct) to convert from one nullable type to another "compatible" nullable type.
Specifically, converting from decimal? to double? can be done using:
public double? ConvertToNullableDouble(decimal? source)
{
return source.HasValue ? Convert.ToDouble(source) : (double?) null;
}
Is there any better way to do this? Maybe leveraging a standard conversion?
In general, if you want o convert from any data type to the other as long as they are compatible, use this:
for example:
A little bit further, if you want it to be more safer, you can add a try catch on it:
this is tested under VS 2010
Built in casts for the win! Just tested this in VS2012 and VS2010:
Just using an explicit cast will cast null to null, and the internal decimal value to double. Success!