Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flags and << operation on enums? C# [duplicate]

Ok so I am new to C#, and for the life of me I cannot comprehend what exactly the below code (from a legacy project) is supposed to do:

[Flags]
public enum EAccountStatus
{
    None = 0,
    FreeServiceApproved = 1 << 0,
    GovernmentAccount = 1 << 1,
    PrivateOrganisationAccount = 1 << 2,
    All = 8
}

What exactly does the << operator do here on the enums? Why do we need this?

like image 783
Joel Min Avatar asked Jun 21 '16 05:06

Joel Min


People also ask

What is Flag in enum?

Enum Flags Attribute The idea of Enum Flags is to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single value. Such enumeration collections are usually manipulated using bitwise operators.

What is the role of flag attribute in enum?

The [Flag] attribute is used when Enum represents a collection of multiple possible values rather than a single value. All the possible combination of values will come. The [Flags] attribute should be used whenever the enumerable represents a collection of possible values, rather than a single value.

How do I add a flag to an enum?

The "|=" operator actually adds a flag to the enum, so the enum now contains two flag bits. You can use "|=" to add bits, while & will test bits without setting them. And Bitwise AND returns a value with 1 in the targeted bit if both values contain the bit.

Can two enums have the same value in C?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.


2 Answers

Behind the scenes, the enumeration is actually an int.
<< is the Bitwise Left Shift Operator
An equivalent way of writing this code is :

[Flags]
public enum EAccountStatus
{
    None = 0,
    FreeServiceApproved = 1,
    GovernmentAccount = 2,
    PrivateOrganisationAccount = 4,
    All = 8
}

Please note, that this enumeration has the Flag attribute

As stated in the msdn:

Use the FlagsAttribute custom attribute for an enumeration only if a bitwise operation (AND, OR, EXCLUSIVE OR) is to be performed on a numeric value.

This way, if you want to have multiple options set you can use:

var combined =  EAccountStatus.FreeServiceApproved  | EAccountStatus.GovernmentAccount 

which is equivalent to:

  00000001  // =1 - FreeServiceApproved 
| 00000010  // =2 - GovernmentAccount 
 ---------
  00000011  //= 3 - FreeServiceApproved  and  GovernmentAccount 

this SO thread has a rather good explanation about the flags attribute

like image 133
Avi Turner Avatar answered Oct 18 '22 04:10

Avi Turner


<< is doing simply what does i.e. Shift left operation.

As far as why in an enum is concerned, its just a way of evaluating the expression as enums allow expressions (and evaluate them on compile time)

like image 23
Failed Scientist Avatar answered Oct 18 '22 04:10

Failed Scientist