Skip to content

[Feature] Enum [Flags] should warn if values not powers of 2 #213

Open
@jzabroski

Description

@jzabroski

The following examples do not follow the Remarks section in System.FlagsAttribute:

Define enumeration constants in powers of two, that is, 1, 2, 4, 8, and so on. This means the individual flags in combined enumeration constants do not overlap.

As a result of this invalid configuration, ToString() returns incorrect results because the Flags values use an unsupported configuration, so the behavior is undefined/non-supported.

    [Flags]
    enum TestEnum
    {
        A = 3, B = 4
    }
    [Flags]
    enum TestEnum2
    {
        A = 3, B = 4, C = 5
    }
    static void Main(string[] args)
    {
        Console.WriteLine(TestEnum.A|TestEnum.B);
        Console.WriteLine(TestEnum2.A|TestEnum2.B);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions