Reputation: 5488
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'
Upvotes: 1
Views: 759
Reputation: 2708
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();
Upvotes: 0
Reputation: 26
yes,you cannot do like this ,both the value should be of same data type.Any specific reason to use tertiary..use if else ...
Upvotes: 0
Reputation: 7629
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;
Upvotes: 5