I have a nullable double
MyNullableDouble = MyDouble == 0 ? null : MyDouble;
This is causing me an issue :
Type of conditional expression cannot be determined because there is no implicit conversion between '' and 'double'
I have a nullable double
MyNullableDouble = MyDouble == 0 ? null : MyDouble;
This is causing me an issue :
Type of conditional expression cannot be determined because there is no implicit conversion between '' and 'double'
you should cast Mydouble, otherwise on the left side you have type double? while in the right part you have double, so types are not equivalent (and that's exactly what the exception is saying):
MyNullableDouble = MyDouble == 0 ? null : (double?)MyDouble;
yes,you cannot do like this ,both the value should be of same data type.Any specific reason to use tertiary..use if else ...
You can implement a generic approach to handle such situations.
Since all Nullable types have GetValueOrDefault method, you can write an opposite method for non-Nullable structures:
public static T? GetNullIfDefault<T>(this T value)
where T: struct
{
if( value.Equals(default(T)))
{
return null;
}
return value;
}
Example of usage:
MyNullableDouble = MyDouble.GetNullIfDefault();