C# Keywords Tutorial Part 27: enum

C# Keywords Tutorial Part 27: enum

C# is a potent programming language that equips developers with a multitude of instruments to develop efficient and scalable code. Among these tools lies the “enum” keyword. This blog article delves into the workings of enums, their purpose and functionality, and their practical applications within C#.

What is an enum in C#?

An enum is a user-defined data type in C# that represents a set of named constants. Enums can be used to group related constants together and provide a more meaningful way to work with them in code. Each constant in an enum is assigned an integer value by default, starting with 0 for the first constant and incrementing by 1 for each subsequent constant.

Declaring an enum

In C#, to define an enum, use the “enum” keyword along with the name of the enum, followed by a set of curly braces encapsulating the constant names. Below is an example:

enum DaysOfWeek
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

In this example, we have declared an enum called “DaysOfWeek” that contains seven constants, each representing a day of the week.

Using an enum

Once you have declared an enum, you can use it in your code just like any other data type. Here is an example of how to use the “DaysOfWeek” enum we declared earlier:

DaysOfWeek today = DaysOfWeek.Tuesday;

if(today == DaysOfWeek.Saturday || today == DaysOfWeek.Sunday)
{
    Console.WriteLine("It's the weekend!");
}
else
{
    Console.WriteLine("It's a weekday.");
}

This code sample declares a variable named “today” of the data type “DaysOfWeek” and assigns it the value “Tuesday”. An if statement is then utilized to determine if “today” falls on either Saturday or Sunday. If it does, the console outputs “It’s the weekend!”; if it doesn’t, it outputs “It’s a weekday.”.

Furthermore, enums can function as parameters for methods and return types. Below is an example of a method that accepts an enum as a parameter:

enum MathOperation
{
    Addition,
    Subtraction,
    Multiplication,
    Division
}

static double Calculate(double x, double y, MathOperation op)
{
    switch(op)
    {
        case MathOperation.Addition:
            return x + y;
        case MathOperation.Subtraction:
            return x - y;
        case MathOperation.Multiplication:
            return x * y;
        case MathOperation.Division:
            return x / y;
        default:
            throw new ArgumentException("Invalid operation specified.", "op");
    }
}

This example demonstrates the declaration of an enum named “MathOperation” with four constants, each representing a different mathematical operation. A method named “Calculate” is then defined with three parameters: two doubles and a MathOperation enum. The method leverages a switch statement to carry out the desired math operation on the two doubles and returns the outcome.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *