Let’s say I have an enum like this:
[Flags]
public enum NotificationMethodType {
Email = 1,
Fax = 2,
Sms = 4
}
And let’s say I have a variable defined as:
NotificationMethodType types = (NotificationMethodType.Email | NotificationMethodType.Fax)
How do I figure out all of the NotificationMethodType values that are not defined in the “types” variable? In other words:
NotificationMethodType notAssigned = NotificationMethodType <that are not> types
If the list of types never changes, you can do this:
The ~ creates an inverse value, by inverting all the bits.
A typical way to define such enums to at least keep the definition of “allTypes” local to the enum would be to include two new names into the enum:
Note: If you go the route of adding the
Allvalue to the enum, note that iftypeswas empty, you would not get an enum that would print as “Email, Fax, Sms”, but rather as “All”.If you don’t want to manually maintain the list of
allTypes, you can do it using theEnum.GetValuesmethod:or you can do the same with LINQ:
This builds the
allTypevalue by OR’ing together all the individual values of the enum.