| by Arround The Web | No comments

Enumeration Types in C#

An enumeration type, is a set of named constants also known as an “enum” for short, each constant has an underlying integer value that starts at zero and increases by one for each subsequent constant, but you can assign specific values to each constant if you prefer.

Enumeration Types in C#

Enums are commonly used to define a set of related values, such as name of week days, names of months in a year, colors, or error codes. They allow you to give meaningful names to values that might otherwise be represented by cryptic integer values or magic numbers in your code. Using enums can make your code more readable and easier to maintain because the names of the constants are self-documenting and the values can be easily changed in one place if needed, here is the syntax for using it:

enum <enum_name> {
    <enumerator1>,
    <enumerator2>,
    ...
}

 

Here, <enum_name> is the name of the enumeration type, and <enumerator1>, <enumerator2>, are the individual values of the enumeration type, here’s an example of how to use enumeration types to represent the different sizes of T-shirts:

using System;

namespace EnumerationDemo
{
    public enum FruitType
    {
        Apple,
        Banana,
        Orange,
        Mango,
        Pineapple,
        Strawberry
    }
    public class Program
    {
        static void Main(string[] args)
        {
            // Declare a variable of type FruitType and assign it a value.
            FruitType myFruit = FruitType.Banana;

            // Print the value of the variable.
            Console.WriteLine(myFruit);

            // Loop through all the values of the FruitType enum.
            foreach (FruitType fruit in Enum.GetValues(typeof(FruitType)))
            {
                Console.WriteLine(fruit);
            }
        }
    }
}

 

The code above demonstrates the use of enumeration in C#. The code begins by importing the System namespace, which includes various system-related classes and methods.

The code then declares an enumeration named FruitType with a list of possible fruit types such as Apple, Banana, Orange, Mango, Pineapple, and Strawberry, after that the Program class is then defined, which contains a static method called Main.

This method serves as the entry point for the application and takes an array of string arguments as input. Within the Main method, a variable called myFruit of type FruitType is declared and assigned a value of FruitType.Banana.

Next, a foreach loop is used to iterate through all the possible values of the FruitType enumeration using the Enum.GetValues method. The current value of the loop is stored in a variable called fruit, which is then printed to the console using the Console.WriteLine method.

The output of the program would be the value of myFruit variable, which is Banana, followed by all the values of the FruitType enumeration.

Conclusion

In C# enumeration types are a useful way to define a group of named constants that have specific integer values assigned to them. Using enumeration types can make your code more readable and easier to maintain. Instead of using numbers, you can use named constants that have a specific meaning. You can also use enumeration types to restrict the values that a variable can take, making your code more robust.

Share Button

Source: linuxhint.com

Leave a Reply