Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# enum exclude

Tags:

c#

enums

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
like image 690
awilinsk Avatar asked Mar 21 '11 11:03

awilinsk


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

If the list of types never changes, you can do this:

NotificationMethodType allTypes = NotificationMethodType.Email |
    NotificationMethodType.Fax |
    NotificationMethodType.Sms;

NotificationMethodType notAssigned = allTypes & ~types;

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:

[Flags]
public enum NotificationMethodType {
    None = 0,
    Email = 1,
    Fax = 2,
    Sms = 4,
    All = Email | Fax | Sms
}

Note: If you go the route of adding the All value to the enum, note that if types was 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 the Enum.GetValues method:

NotificationMethodType allTypes = 0;
foreach (NotificationMethodType type in Enum.GetValues(typeof(NotificationMethodType)))
    allTypes |= type;

or you can do the same with LINQ:

NotificationMethodType allTypes = 
    Enum.GetValues(typeof(NotificationMethodType))
    .Cast<NotificationMethodType>()
    .Aggregate ((current, value) => current | value);

This builds the allType value by OR'ing together all the individual values of the enum.

like image 75
Lasse V. Karlsen Avatar answered Oct 06 '22 21:10

Lasse V. Karlsen


A simple XOR will do the trick...

NotificationMethodType all = (NotificationMethodType.Email | NotificationMethodType.Fax | NotificationMethodType.Sms);
NotificationMethodType used = (NotificationMethodType.Email | NotificationMethodType.Fax);
NotificationMethodType unused = (all ^ used);

To make this a little cleaner, add the All value to your enum definition directly (set value to 7 obviously). This way, you can add things to the enum later without breaking this code

like image 22
Robert Levy Avatar answered Oct 06 '22 20:10

Robert Levy