| by Arround The Web | No comments

Associating Enums with Strings in C#

Enums in C# are used to define a set of named constant values that can be assigned to variables. They are useful in defining a fixed set of options for a particular variable or parameter. However, when it comes to displaying the enum options to the user, it’s not always ideal to use the enum names directly. Associating the enum values with corresponding strings can make the code more readable and user-friendly. In this article, we’ll discuss how to associate enums with strings in C# with an example code.

Associating enums with strings in C#

To associate an enum with a string, we can use the Description attribute. This attribute allows us to attach a string to each value of an enum. Here’s an example:

public enum EnumName
{
    [Description("String Value")]
    EnumValue
}

 

In this syntax, we first define our enum with the name “EnumName”. Then, we add the Description attribute to each value of the enum. Inside the attribute, we add the corresponding string value for the enum value further below is a c# code that illustrates associating enum with strings:

using System;
using System.ComponentModel;

public enum car
{
    [Description("Best Electric car manufacturer")]
    BMW,
    [Description("Most Reliable cars")]
    TOYOTA,
    [Description("Most Safest cars to Drive")]
    VOLVO
}
public static class EnumExtensions
{
    public static string GetDescription(this Enum value)
    {
        var field = value.GetType().GetField(value.ToString());
        var attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
        return attributes.Length > 0 ? attributes[0].Description : value.ToString();
    }
}
public class Program
{
    public static void Main()
    {
        car mycar = car.VOLVO;
        string mycarString = mycar.GetDescription();
        Console.WriteLine(mycarString);
    }
}

 

he above code is an example of how to associate enums with strings in C#. Here, we have defined an enum “car” with three values – BMW, TOYOTA, and VOLVO. Each of these values is associated with a corresponding string using the Description attribute.

We have also defined a static class “EnumExtensions” that contains a method “GetDescription” to get the corresponding string value of the enum. The method works by first getting the field info for the enum value using reflection, and then getting the Description attribute from that field info. If the attribute exists, it returns its value; otherwise, it just returns the name of the enum value.

Finally, in our Main method, we create a variable “mycar” of type car and assign it the value car.VOLVO. We then call the GetDescription method on “mycar” and assign the result to “mycarString”. Finally, we print the value of “mycarString” to the console, which outputs “Most Safest cars to Drive”. This demonstrates how we can easily associate enum values with string values using the Description attribute and retrieve the corresponding string value when needed.

Conclusion

Associating enums with strings in C# can make our code more readable and user-friendly and by using the Description attribute and the GetDescription method, we can easily associate string values with each enum value and retrieve the corresponding string when needed. This technique can be especially useful in user interfaces or when working with external data sources that use string values instead of enum values.

Share Button

Source: linuxhint.com

Leave a Reply