mardi 31 juillet 2018

Why does IsLiteral return false for decimal?

The following program will print the fields and whether the are constant or not using IsLiteral

public static class Program
{
    public static void Main(string[] args)
    {
        foreach (var field in typeof(Program).GetFields())
        {
            System.Console.WriteLine(field.Name + " IsLiteral: " + field.IsLiteral);
        }

        System.Console.ReadLine();
    }

    public const decimal DecimalConstant = 99M;
    public const string StringConstant = "StringConstant";
    public const int IntConstant = 1;
    public const double DoubleConstant = 1D;
}

It works correctly for all types, except for decimal is will return false.

Can anyone explain this behavior? And is there a better way to see if a field in constant?





Aucun commentaire:

Enregistrer un commentaire