Why c# decimals can't be initialized without the M suffix?

后端 未结 6 1121
一生所求
一生所求 2020-11-30 11:56
public class MyClass
{
    public const Decimal CONSTANT = 0.50; // ERROR CS0664   
}

produces this error:

error CS0664: Lit

6条回答
  •  抹茶落季
    2020-11-30 12:44

    The first example is a double literal. The second example is an integer literal.

    I guess it's not possible to convert double to decimal without possible loss of precision, but it is ok with an integer. So they allow implicit conversion with an integer.

提交回复
热议问题