| by Arround The Web | No comments

C# Enumeration

C# is the programming language that is used to develop programs, websites, android applications, games, and many more. C# supports different libraries and frameworks that have built-in functions. Enum is an enumeration, it is a class. A class has different data types and functions in it, a data structure, which contains different variables in it. Enumeration is particularly useful when we know all the variables at compile time which a user defines the data types in it. Enum contains different values and those values cannot be changed. Values can be retrieved and used in code but we cannot change the values on run time these are only readable and unchangeable. Enumeration treats all the data in it as a string, if we want to use an integer value we need to type cast. Enumeration is useful because they take less memory and space.

Syntax

 

enum E_Name{ Value_1, Value_2, Value_3,…..};

 
Or

enum E_Name{ Value_1 = 34 , Value_2 = 90 , Value_3 = 8 ,…..};

 
The attribute ‘enum’ is the keyword to define enumeration. Whenever we need user-defined data types, ‘enum’ keyword is used to create the user-defined data type. The ‘E_name’ is the name of the enum class. Inside this class curly braces will contain all the parameters and values in it. We can only write the arguments and the compiler will assign them values. By default, starting from the first value stores in the index 0 and the next on 1 and so on.

There is no restriction on the data type of the arguments, we can assign integer values, floating point values, characters, Boolean, or string. But if we do not assign the data types to the values, it will automatically assign them the constant value where they are stored.

Types

In the C# language, there are two types of enumeration. These are listed below:

    • Simple enumeration
    • Flag enumeration

Simple Enumeration

In this category, the members of the ‘enum’ class contain a single value.

Flag Enumeration

In this category, the member of the flag enum class contains multiple values or multiple values using the bitwise operator or using OR (|).

Example 1:

In this instance, we will learn how we can use the simple ‘enum’ class and retrieve the values without assigning them to variables.


For coding in C# programming language, we need to first create the new project and then start coding. When we want to create a console application, we should select the console application, C# framework, that will automatically write the class and main() function. To use the simple term ‘enum’ above in the code, we will define and initialize the internal class ‘Program’. As the enum is the keyword and the very next word after the enum is ‘fruit_names’. Inside the curly braces, we define the list we want. Here, enum class contains four fruits names. We call all the functions inside the main() method. Inside the static main(), we will display one after another all the enum values. In this code, we just write the enum arguments, not the values. So, it will automatically assign them the indexes where they are stored as the value.

Console.writeline() function is used to write on the output screen and after presenting the message, it moves the cursor to the next line. Inside this statement, write a message and concatenate it with a ‘+’ sign to show that defined message with the value. After concatenating type ‘enum name’ and use (.) to address a specific type of ‘enum’: fruit_names.Apple. Then, in the next statement, again utilize the (+) sign to concatenate the type with the message and index where it is stored. To get the index, we first need to convert it into an integer by doing typecasting. In C# type, casting is done by just using the parenthesis. Inside the parenthesis, write the data type in which we want to typecast. Outside the parenthesis, write the variable name i.e. (int)var_name. In this way, we can get the message with an enum value and index where it is being stored.

Repeat this three times; just change the message and enum type. Enumeration makes code look nice and clean. The Console.ReadKey() function will keep the console active until we close it. If we do not use this statement, the console blinks once and then it will disappear.

Example 2:

In this program, we will implement the simple ‘enum’ class by assigning different values to the attributes of the class.


In the second example, we again created a project for the console application and defined an enumeration inside the internal class ‘Program’ with the keyword ‘enum’. This time it contains the class ‘items’ with different values. Inside the main() function, we will access all the elements of the enum class ‘items’. Let us utilize the console.writeLine() method. It will display everything it has in it. First, display a message, then again call the Console.WriteLine() function to display the first item with the quantity. Define a text we want to display on the terminal and concatenate it with the items.chairs. It will get the first item (chairs) of the enum class ‘items’. To get the quantity of this item, we need to do casting because the data in the enum class is in the form of a string. So, to access their values we first type cast. To get the value of ‘chairs’, do casting like (int)items.chairs. Otherwise, we will get only the item ‘chairs’ not its value. Here, if we noticed, the symbols ‘\n’ and ‘\t’ is used. By using ‘\t’, we give a tab space and by using the ‘\n’ we will move the cursor to the next line. Repeat this procedure twice. We have to just update the enum values. In the end, just write the console.ReadKey() to keep the console active so that we can see the output.

Example 3:

We are going to utilize the ‘flag’ enum in this scenario.


In simple enumeration, we can only assign one value to the member of the enum class. But with flag enumeration, we can assign more than one value to the one member of the enum class. This type of enumeration is helpful when we deal with different choices and when we have more than one value. To use flag enumeration, before defining an enum class, we have to write [flags] and then write enum class ‘flag_example’. Now, inside this class, we can easily assign more than one value to one member. In this example, we assigned two values to the attribute ‘driver’ and separated both values by using the (|) operator. Then, in the main() function declare a variable ‘var flags’. To show more than one item on the console, utilize or (|) operator and then call the enum items with the class name and item name. To display a message, invoke the Console.WriteLine() method. To represent the value of the flag, write ‘flag’ in another Console.Write(). In the next statement the Console.ReadKey() function is called that will keep the console open until we close it.

Conclusion

In this guide, we have learned about enumeration, what it is and how we can utilize it in C# language. Enumeration makes the code simple and readable. Because values in the enum class cannot be changed. It provides us benefits when we are dealing with such kind of data that does not require changing values. We have discussed syntax and types of enumeration with the help of different codes. Enumeration takes less memory and is easy to use.

Share Button

Source: linuxhint.com

Leave a Reply