In this article, Mytour will introduce you to Enum in C# and how to initialize Enum in C#.
Explore Enum in C#
1. Introduction to Enum in C#.
1.1. Syntax for declaring enum in C#.
1.2. Example of Enum in C#.
2. Initializing Enum.
2.1. Example of initializing Enum.
3. Changing data type of Enum members.
3.1. Example of changing data type.
1. Enum in C#
Enumeration (or Enum) in C# is a value type data, used to assign names or string values to integer constants so that the program can easily read and maintain them. For example, the four suits: clubs, diamonds, hearts, and spades in a deck of cards are also an Enum.
Enum is used to define data types (enumerated data types), and is declared by using the enum keyword directly within the namespace, class, or structure.
1.1. Syntax for declaring enum in C#
The syntax for declaring enum is as follows:
In the above syntax, Enum_variable is the variable name of the enum, and string_1 is assigned the value 0, string_2 is assigned the value 1, ... . By default, the first member of the enum has a value of 0, and each subsequent enum member value increases by 1 unit, however, we can change this default value.
1.2. Example of Enum in C#
The output is in the following format:
As mentioned earlier, the default value of the first enum member is set to 0, then incremented by 1 for subsequent enum members. However, you can modify these default values if desired.
2.1. Example of Enum Initialization:
In the example above, day1 is assigned the value 1 by the user, day2 is assigned the value 2, and likewise with member day3. Therefore, you only need to change the value of the first enum member; subsequent enum members will increase by 1 compared to the previous value.
Note: If the data of an enum member has not been initialized, its value is set according to the following rules:
- If it is the first member, its value will be set to 0 if not.
- It sets the value by adding 1 to the previous value of the enum data member.
2.2. Another example of enum initialization:
Here A is set to a value of 0 by default, B is incremented by 1 value. However, because C is initialized to 6, the value of D will be 7.
Example: The following example illustrates initializing enum data members with values defined by the user and some special cases when initializing enums.
The output format is as follows:
Explanation: In the code snippet above, there are 2 enums: color and days. In the case of the days enum, initialization is not performed. Therefore, according to the rule, Monday is assigned a value of 0, incrementing by 1 for Tuesday, Wednesday, and other days.
However, in the case of the color enum, Red will be assigned a value of 0, Yellow incremented by 1, similarly with Blue. In the case of Green, its value is assigned by adding 1 to the value of Yellow, which is 5, so the resulting value is 6. Similarly, in the case of Brown, its value will be 7, and in the case of Black, its value will be 10 (7 + 3).
3. Changing Enum Member Data Type
By default, the underlying data type of an enum in C# is int. However, users can change it to bool, long, double, ... if desired.
3.1. Example of Changing Enum Member Data Type in C#:
Program: The following program illustrates the change of data type of Enum members.
Input:
The output format is as follows:
So here Mytour has just introduced you to Enums in C#. In addition, readers can also refer to some other articles on Mytour to learn more about the structure in C# and string in C#.