Enumeration in c# with example

Enumeration in c# with example


Enumeration in C# with Example

Enumerations, or enums, are a special "class" in C# that represents a group of named integer constants. Used to define variables that can only assign certain predefined constants, enums enhance code clarity and enforce type safety. This article explores various aspects of using enumerations in C# including how to define them, assign values, and utilize them in applications.

Example of Enumeration in C# with Example

Defining an enum in C# is straightforward. Here is a simple example:

 

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

Day today = Day.Wednesday;
Console.WriteLine(today); // Outputs "Wednesday"

Enumeration in C# with Example W3Schools

Following a similar structure, here's an extended example that includes using enums in a more practical context, such as in a switch statement:

 

public enum CoffeeSize { Small, Medium, Large }

public class Coffee
{
    public CoffeeSize Size;

    public Coffee(CoffeeSize size)
    {
        Size = size;
    }
}

var myCoffee = new Coffee(CoffeeSize.Medium);
switch (myCoffee.Size)
{
    case CoffeeSize.Small:
        Console.WriteLine("Small coffee");
        break;
    case CoffeeSize.Medium:
        Console.WriteLine("Medium coffee");
        break;
    case CoffeeSize.Large:
        Console.WriteLine("Large coffee");
        break;
}

C# Enum with Values

You can specify the underlying values of the enums, which by default are integers starting from 0. Here’s how to explicitly set them:

 

public enum StatusCode { Success = 200, NotFound = 404, Error = 500 };

StatusCode code = StatusCode.Success;
Console.WriteLine((int)code); // Outputs "200"

Enum in C

In C, enums are also used to define sets of named integer constants but are less type-safe compared to C#. Here is a basic example for comparison:

 

enum Color { Red, Green, Blue };
Color favoriteColor = Green;
printf("%d", favoriteColor); // Outputs "1"

C# Enum Methods

Enums in C# can use static methods from the Enum class to perform various tasks, like parsing or getting values:

 

string status = "NotFound";
StatusCode code = (StatusCode)Enum.Parse(typeof(StatusCode), status);
Console.WriteLine((int)code); // Outputs "404"

C# Enum Naming Convention

Enums are typically named with singular nouns if they represent a single item or singular entity. Enum members should be PascalCase and be as descriptive as necessary to convey their purpose clearly.

C# Enum with String Values

While you cannot directly assign string values to enum members, you can use attributes or helper methods to associate strings with them:

 

public enum Fruit
{
    [Description("Apple Fruit")]
    Apple,
    [Description("Banana Fruit")]
    Banana
}

public static string GetDescription(Fruit fruit)
{
    var type = typeof(Fruit);
    var memInfo = type.GetMember(fruit.ToString());
    var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
    return ((DescriptionAttribute)attributes[0]).Description;
}

Fruit myFruit = Fruit.Apple;
Console.WriteLine(GetDescription(myFruit)); // Outputs "Apple Fruit"

C# Enumeration Class

C# doesn't support the concept of an enumeration class directly. Enums are defined as their own type but can be enriched with attributes to add additional metadata or functionality.

Enumerations are fundamental in C# for creating readable, maintainable code that reduces the likelihood of invalid values and simplifies debugging.


 

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

Popular Posts

Categories Clouds