I have the following enum
[Flags]
public enum Anchor
{
None = 1,
Top = 2, Bottom = 4,
Left = 8, Right = 16
}
I need to test every possible combination of Top, Bottom, Left and Right but declaring all those combinations in a switch is awful.
switch (anchor)
{
case Anchor.Left:
Thing1();
break;
case Anchor.Top:
Thing2();
break;
case Anchor.Left | Anchor.Top:
Thing3();
break;
}
This question is not a duplicate of this or this . I've tried both solutions and neither works for me because if my enum is, say Anchor.Left | Anchor.Top
Anchor.Left | Anchor.Top
, Thing1()
and Thing2()
would be called whereas I need Thing3()
to be called.
Declare all these combinations in a switch. (as you started)
Its clear, understandable, maintainable.
Not awful. (Feels just a bit stupid to key it in)
After all, you have these distict cases, so a switch is the perfect construction.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.