C# Enum Summary

Posted by TimTimTimma on Sat, 18 May 2019 21:31:57 +0200

Enumeration concept

Enumeration types (also known as enumerations) provide an effective way to define a set of named integer constants that may be assigned to variables. This type is declared using the enum keyword.

Example code 1

enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

By default, the basic type of each element in the enumeration is int. You can use a colon to specify another integer type.

Example code 2

enum Day : byte {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

By default, the first enumeration value has a value of 0, and the value of each successive enumeration increases by 1.

Enumerators can replace default values with initial value settings.

If the value of an enumerator is set, the subsequent enumerators will still increase by 1.

Example code 3

enum Day {Sat=1, Sun, Mon, Tue=5, Wed, Thu, Fri};

Each enumeration has a base type, which can be any integer type other than char. The default base type for enumeration elements is int.

The approved enumeration types are byte, sbyte, short, ushort, int, uint, long or ulong.

You can assign any value to an element in an enumerator list of enumeration types, or you can use calculated values.

Example code 4

enum MachineState
{
    PowerOff = 0,
    Running = 5,
    Sleeping = 10,
    Hibernating = Sleeping + 5
}

enumeration method

Get name

public static string GetName(Type enumType, object value);

Example code 5

//Get the name of Day.Friday in the Day enumeration and return the value "Friday"
System.Enum.GetName(typeof(Day), Day.Friday)

Get the name array

public static string[] GetNames(Type enumType);

Example code 6

string[] names = System.Enum.GetNames(typeof(Day));

Getting instances worth arrays

public static Array GetValues(Type enumType);

Example code 7

Array arr = System.Enum.GetValues(typeof(Day));

Converts the string representation of the name or value of an enumeration constant to an equivalent enumeration object

public static object Parse(Type enumType, string value);

Example code 8

string day = Day.Friday.ToString();
var fri = (Day)System.Enum.Parse(typeof(Day),day);

Relevant common methods can refer to the enumeration base class System.Enum.

Enumeration Types as Bit Markers

Enumeration types can be used to define bit flags, which enables instances of enumeration types to store any combination of values defined in the enumerator list. (Certainly, some combinations may not be meaningful or allowed in your program code.)

The way to create a bit flag enumeration is to apply System.FlagsAttribute Attribute and define some values appropriately so that AND, OR, NOT and XOR bitwise operations can be performed on these values. In the in-place flag enumeration, a named constant with a value of zero (meaning "no flag is set") is included. If a zero value does not mean "no flag is set", do not specify a zero value for the flag.

Example code 9

[Flags]
enum Days
{
    None = 0x0,
    Sunday = 0x1,
    Monday = 0x2,
    Tuesday = 0x4,
    Wednesday = 0x8,
    Thursday = 0x10,
    Friday = 0x20,
    Saturday = 0x40
}

Enumeration Bit Operations

| union set & intersection ^ difference set

Expanding new methods for enumeration

Example code 10

 

[Display(Name = "a week")]
public enum Day
{
  [Display(Name = "Sunday")]
  Sunday,
  [Display(Name = "Monday")]
  Monday,
  [Display(Name = "Tuesday")]
  Tuesday,
  [Display(Name = "Wednesday")]
  Wednesday,
  [Display(Name = "Thursday")]
  Thursday,
  [Display(Name = "Friday")]
  Friday,
  [Display(Name = "Saturday")]
  Saturday
}
/// <summary>
/// Enumeration of Extended Classes
/// </summary>
public static class EnumExtend
{
  /// <summary>
  /// Get display text according to DisplayAttribute feature under System. ComponentModel. Data Annotations
  /// </summary>
  /// <param name="t"></param>
  /// <returns></returns>
  public static string GetDisplayText(this Enum t)
  {
    var t_type = t.GetType();
    var fieldName = Enum.GetName(t_type, t);
    var objs = t_type.GetField(fieldName).GetCustomAttributes(typeof(DisplayAttribute), false);
    return objs.Length > 0 ? ((DisplayAttribute)objs[0]).Name : null;
  }
}

 

  

Reference: Enumeration Types (C# Programming Guide)

Topics: C# Attribute Programming