C #, Operator '??' cannot be applied to operands of the type "decimal" and "decimal",

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?

+8
source share
4 answers

The decimal type cannot be null, so the null-coalesce operator does not make sense here. Just set _v1 to value .

+13
source share

These are value types and cannot be null you can use Nullable<decimal>

 private decimal? _v1; public decimal? V1 { get { return this._v1; } set { this._v1 = value ?? 0M; } } 
+5
source share

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:

 public decimal? v1 { get { return this._v1; } set { this._v1 = value ?? 0M; } } 
+1
source share

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

0
source share

All Articles