C# Enums and Bit Enums
Category Programming Techniques
1. Concept of Enums:
C# Enums (Enum), enumeration types are used to declare a set of named constants of a basic data type (value type);
2. Definition of Enums:
Declare enum variable:
enum <enum_name> {enumeration list};
Where enum_name specifies the type name of the enum, and enumeration list is a comma-separated list of identifiers;
Each symbol in the enumeration list represents an integer value, one greater than the integer value of the symbol preceding it. By default, the value of the first enumeration symbol is 0. For example:
enum Days {Sun, Mon, tue, Fir, sat};
3. Examples of Enums, 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("Friday: {0}", WeekdayEnd);
Console.ReadKey();
}
}
}
4. Enum Class:
All enumeration types implicitly inherit the System.Enum type, which is the only reference type not a value type inheriting from System.ValueType;
5. Enum Class:
Method | Description |
---|---|
CompareTo | Compares this instance to a specified object and returns an indication of their relative values |
Equals | Indicates whether this instance is equal to a specified object |
Format | Converts the specified value of a specified enumerated type to its equivalent string representation according to the specified format |
GetName | Retrieves the name of the constant in the specified enumeration that has the specified value |
GetNames | Retrieves an array of the names of the constants in a specified enumeration |
GetTypeCode | Returns the underlying TypeCode for this instance |
GetUnderlyingType | Returns the underlying type of the specified enumeration |
GetValues | Retrieves an array of the values of the constants in a specified enumeration |
HasFlag | Determines whether one or more bit fields are set in the current instance |
IsDefined | Returns an indication whether a constant with a specified value exists in a specified enumeration |
Parse | Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object, a parameter specifies whether the operation is case-insensitive |
TryParse | Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object, a return value indicates whether the conversion succeeded |
6. Flags Enum/Bit Enum
C# Flags Enum (Flags): Enumeration types are used to declare a set of named constants of a basic data type (value type).
Enum values are mutually exclusive. Bit flag collections are lists formed by combined elements, usually designed to combine new values with a "bitwise OR" operation.
Enumeration types typically express a set of semantically independent values. Using enumeration types to implement bit flag collections is the perfect combination,简称位枚举.
7. Definition of Bit Enum
///<summary>
/// Permission Enum
///</summary>
[Flags]
public enum Permission
{
Unknown = 0, // Can also be written as 0x00 or 0
Create = 1 << 0, // 0x01 or 1
Read = 1 << 1, // 0x02 or 2
Update = 1 << 2, // 0x04 or 4
Delete = 1 << 3 // 0x08 or 8
}
8. Example of Bit Enum
// 1. Grant user create, read, update, and delete permissions
var permission = Permission.Create | permission.Read | permission.Update | permission.Delete;
// 2. Remove user's update and delete permissions
permission = permission & ~permission.Update;
permission = permission & ~permission.Delete;
// 3. Grant user update permission
permission = permission | permission.Update;
// 4. Check if user has create permission
var isCreate = (permission & permission.Create) != 0;
// or
var isCreate = (permission & permission.Create) == permission.Create;
Original Source: https://www.cnblogs.com/chenyao-1424433719/p/11213658.html