Pattern: Incorrect enum argument to Enum.HasFlag()
Issue: -
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);
}
}