1. Concept of Enums:

C# Enum: An enum type is a basic data type (value type) used to declare a set of named constants.

2. Definition of Enums:

Declaring an enum variable:

enum <enum_name> {enumeration list};

Here, enum_name specifies the type name of the enum, and enumeration list is a comma-separated list of identifiers.

Each symbol in the enum list represents an integer value, one greater than the symbol preceding it. By default, the first enum symbol has a value of 0. For example:

 
enum Days {Sun , Mon, tue, Fir, sat };

3. Enum cases and usage of enum variables:

Example

using System;
namespace EnumApplication
{
    class EnumProgram
    {
          enum Days { Sun, Mon, tue, Wed, thu, Fri, sat };
         
          static void Main(string[] args)
          {
                int WeekdayStart = (int)Days.Mon;
                int WeekdayEnd = (int)Days.Fir;
                Console.WriteLine("Monday:{0}",WeekdayStart);  
                Console.WriteLine("Firday:{0}",WeekdayEnd );  
                Console.ReadKey();
          }    
    }
}

4. Enum Class:

All enum types implicitly inherit the System.Enum type. System.Enum is the only reference type derived from System.ValueType that is not a value type.

5. Enum Class:

MethodDescription
CompareToCompares this instance to a specified object and returns an indication of their relative values.
Equals: Indicates whether this instance equals a specified object.
FormatConverts the specified value of the specified enum type to its equivalent string representation according to the specified format.
GetNameRetrieves the name of the constant that has the specified value in the specified enum.
GetNamesRetrieves an array of the names of the constants in the specified enum.
GetTypeCodeReturns the underlying TypeCode of this instance.
GetUnderlyingTyeReturns the underlying type of the specified enum.
GetValuesAn array of the values of the constants in the specified enum.
HasFlagDetermines whether one or more bit fields are set in the current instance.
IsDefinedReturns an indication of whether a constant with the specified value exists in the specified enum.
ParseConverts the string representation of the name or numeric value of one or more enum constants to an equivalent enum object; a parameter specifies whether the operation is case-insensitive.
TryParseConverts the string representation of the name or numeric value of one or more enum constants to an equivalent enum object; a return value indicates whether the conversion succeeded.

6. Flag Enum / Bit Enum

C# Flag Enum (Flags): An enum type is a basic type of data (value type) used to declare a set of named constants.

Enum values are mutually exclusive. A bit flag set, however, is a list formed by elements that appear in combinations, usually designed to combine new values using a "bitwise OR" operation.

  

An enum type usually expresses a semantically relatively independent set of numeric values. Using an enum type to implement a bit flag set is the most perfect combination, abbreviated as a bit enum.

7. Definition of Bit Enum

///<summary>
  /// 权限枚举
  ///</summary>
  [Flags]
  public enum permission                     注意加了[Flags]特性后有三种写法,
  {                                  一种是使用<<符号,
      Unknown = 0; // 也可以写成0x00或0             第二种是0x01,                
                                    还有一种是直接写0,1,2,4,8....,
      Create = 1<<0, // 0x01或1                 一般来说是2的n次方来表示。  
                                     注:<<左操作符,表示对这个数进行移位。
      Read = 1<<1,  //0x02或2

      Update = 1<<2, //0x04或4

      Delete = 1<<3  //0x08或8
  }

8. Example of Bit Enum

//1、给用户创建、读取,修改和删除的权限
var parmission = Permission.Create | parmission.Read | parmission.Update | parmission.Delete;

//2、去掉用户的修改和删除权限
parmission = parmission &~parmission.Update;
parmission = parmission &~parmission.Delete;

//3、给用户加上修改的权限
parmission = parmission | parmission.Update;

//4、判断用户是否有创建的权限
var isCreate = (parmission & parmission.Create)!=0;
//或者
var isCreate = (parmission & parmission.Create)==parmission.Create;
  这时parmission枚举的值将变成0+1+4=5,它的ToSting()将变成“parmission.Create,
  parmission.Read”,parmission.Update; 这里我们可以解释为什么第五个值Delete是8而不能成为5。
  也就是说它的值不应该是前几项值的复合值。一个比较简单的方法
  就是用2的n次方来依次位每一项赋值,例如:1,2,4,8,16,32,64.......。

Original address: https://www.cnblogs.com/chenyao-1424433719/p/11213658.html