Trent
Trent

Reputation: 1593

Accepted way to do Enum comparison

I'm sure this has probably been asked, but I can't seem to find the right answer...maybe just using the wrong search terms. Anyway, I have the following code;

if (e.Menu.Id.Equals(SchedulerMenuItemId.AppointmentMenu) ||
    e.Menu.Id.Equals(SchedulerMenuItemId.AppointmentDependencyMenu))
{ ... }

It seems a little redundant/duplicated to me. I can't use a bitwise OR though, as the enum isn't marked as Flag. I guess that's because there's 71 enum values defined and that's a bit beyond the Flag option...

Potentially I could do a switch and have the cases "fall through" which is maybe a little cleaner...

Is there any other way of doing a comparison such as the above (with extensibility and readability in mind), it may be more than just 2 comparisons.

Upvotes: 1

Views: 92

Answers (1)

Selman Genç
Selman Genç

Reputation: 101681

You can always use LINQ

var enums = new[]
        {
            SchedulerMenuItemId.AppointmentMenu
            SchedulerMenuItemId.AppointmentDependencyMenu
        };

if(enums.Any(x => (int)x == e.Menu.Id)) { }

Upvotes: 6

Related Questions