Skip to content

Files

Latest commit

 

History

History
47 lines (34 loc) · 939 Bytes

File metadata and controls

47 lines (34 loc) · 939 Bytes

Pattern: Incorrect enum argument to Enum.HasFlag()

Issue: -

Description

The Enum.HasFlag method expects the enum argument to be of the same enum type as the instance on which the method is invoked. If these are different enum types, an unhandled exception will be thrown at run time.

Example of incorrect code:

public class TestClass
{
    [Flags]
    public enum FirstEnum { A, B, }

    [Flags]
    public enum SecondEnum { A, }

    public void Method(FirstEnum firstEnum)
    {
        firstEnum.HasFlag(SecondEnum.A);
    }
}

Example of correct code:

public class TestClass
{
    [Flags]
    public enum FirstEnum { A, B, }

    [Flags]
    public enum SecondEnum { A, }

    public void Method(FirstEnum firstEnum)
    {
        firstEnum.HasFlag(FirstEnum.A);
    }
}

Further Reading