public decimal v1 { get { return this._v1; } set { this._v1 = value ?? 0M; // also I tried, default(decimal) } }
Error message:
The operator '??' cannot be applied to operands of the type decimal and decimal
Why does this not work, and how to make it work?
The decimal type cannot be null, so the null-coalesce operator does not make sense here. Just set _v1 to value .
decimal
_v1
value
These are value types and cannot be null you can use Nullable<decimal>
null
Nullable<decimal>
private decimal? _v1; public decimal? V1 { get { return this._v1; } set { this._v1 = value ?? 0M; } }
This is the Null Coalescing Operator . Since the decimal value cannot be zero, it does not make sense with a decimal point.
Can you use decimal? which can be set to null if you need this function:
decimal?
public decimal? v1 { get { return this._v1; } set { this._v1 = value ?? 0M; } }
is it decimal? or decimal
works with decimal point? but not a decimal number, since a decimal value can never be zero.
http://msdn.microsoft.com/en-us/library/ms173224.aspx