הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
| Property | Value |
|---|---|
| Rule ID | CA2248 |
| Title | Provide correct enum argument to Enum.HasFlag |
| Category | Usage |
| Fix is breaking or non-breaking | Non-breaking |
| Enabled by default in .NET 10 | As suggestion |
| Applicable languages | C# and Visual Basic |
Cause
The enum type passed as an argument to the HasFlag method call is different from the calling enum type.
Rule 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 runtime.
How to fix violations
To fix violations, use the same enum type on both the argument and the caller:
public class C
{
[Flags]
public enum MyEnum { A, B, }
[Flags]
public enum OtherEnum { A, }
public void Method(MyEnum m)
{
m.HasFlag(OtherEnum.A); // Enum types are different, this call will cause an `ArgumentException` to be thrown at runtime
m.HasFlag(MyEnum.A); // Valid call
}
}
When to suppress warnings
Do not suppress violations from this rule.
שתף איתנו פעולה ב- GitHub
ניתן למצוא את המקור לתוכן זה ב- GitHub, שם ניתן גם ליצור ולסקור בעיות ולמשוך בקשות. לקבלת מידע נוסף, עיין במדריך התורמים שלנו.