I want to do something like this but C# doesn’t accept this:
public static void setPrice(Double? value)
{
if (value != null) {
this.TextBoxPrice.Text = Math.Round(value, 2).ToString();
} else {
this.TextBoxPrice.Text = "No Price";
}
}
So does it mean using nullable type Double? is completely useless in this use case ? What can I use then ?
Update: I made a mystypo I actually meant
public static void setPrice(Double? value)
so I corrected.
You’re not currently using a nullable type –
Doubleis a non-nullable value type, so can never be null. This is what you want:On the other hand, you should not be using a
doubleto represent financial values. It’s inappropriate as a binary floating point type. Usedecimal(ordecimal?) instead.(Note that
Double?is equivalent toNullable<Double>.)If you’ve come from a Java background, you may be expecting
Doubleto be a reference “wrapper” type to start with – it’s not. In C#doubleis simply an alias forSystem.Double; they’re the same type.