How to define an enum with string value in C#?

How to define an enum with string value in C#?

In C#, you can define an enumeration with string values by using the EnumMember attribute from the System.Runtime.Serialization namespace.

Here is an example:

 using System.Runtime.Serialization; public enum Fruit { [EnumMember(Value = "Apple")] Apple, [EnumMember(Value = "Banana")] Banana, [EnumMember(Value = "Orange")] Orange } 

In this example, the Fruit enumeration has three values, Apple, Banana, and Orange, each with a corresponding string value specified by the EnumMember attribute.

To use these string values in your code, you can use the EnumMemberAttribute.Value property:

 var appleValue = Fruit.Apple.GetAttributeOfType<EnumMemberAttribute>().Value; // "Apple" 

Note that you will need to include the following using directive at the top of your code file:

 using System.Runtime.Serialization; 

Examples

  1. "C# enum with string values using attributes"

    public enum MyEnum { [StringValue("Value1")] EnumValue1, [StringValue("Value2")] EnumValue2, // Add more enum values with string representations } 

    Description: Defines an enum MyEnum with string values using a custom attribute (StringValue).

  2. "C# enum with string values using a base class"

    public abstract class StringValuedEnum { public string Value { get; } protected StringValuedEnum(string value) { Value = value; } } public class MyEnum : StringValuedEnum { public static readonly MyEnum EnumValue1 = new MyEnum("Value1"); public static readonly MyEnum EnumValue2 = new MyEnum("Value2"); // Add more enum values with string representations private MyEnum(string value) : base(value) { } } 

    Description: Defines an enum MyEnum inheriting from a base class StringValuedEnum with string values.

  3. "C# enum with string values using a static class"

    public static class MyEnum { public const string EnumValue1 = "Value1"; public const string EnumValue2 = "Value2"; // Add more enum values with string representations } 

    Description: Defines an enum-like structure using a static class MyEnum with string values.

  4. "C# enum with string values using a Dictionary"

    public static class MyEnum { public static readonly Dictionary<string, string> Values = new Dictionary<string, string> { { "EnumValue1", "Value1" }, { "EnumValue2", "Value2" }, // Add more enum values with string representations }; } 

    Description: Defines an enum-like structure using a Dictionary within a static class MyEnum with string values.

  5. "C# enum with string values using .NET 6.0 [EnumMember] attribute"

    public enum MyEnum { [EnumMember(Value = "Value1")] EnumValue1, [EnumMember(Value = "Value2")] EnumValue2, // Add more enum values with string representations } 

    Description: Defines an enum MyEnum with string values using the [EnumMember] attribute introduced in .NET 6.0.

  6. "C# enum with string values and display names using [Display] attribute"

    using System.ComponentModel.DataAnnotations; public enum MyEnum { [Display(Name = "Value 1")] EnumValue1, [Display(Name = "Value 2")] EnumValue2, // Add more enum values with display names } 

    Description: Defines an enum MyEnum with string values and display names using the [Display] attribute.

  7. "C# enum with string values and descriptions using [Description] attribute"

    using System.ComponentModel; public enum MyEnum { [Description("This is Value 1")] EnumValue1, [Description("This is Value 2")] EnumValue2, // Add more enum values with descriptions } 

    Description: Defines an enum MyEnum with string values and descriptions using the [Description] attribute.

  8. "C# enum with string values and custom attribute for additional metadata"

    public class StringValueAttribute : Attribute { public string Value { get; } public StringValueAttribute(string value) { Value = value; } } public enum MyEnum { [StringValue("CustomValue1")] EnumValue1, [StringValue("CustomValue2")] EnumValue2, // Add more enum values with additional metadata } 

    Description: Defines an enum MyEnum with string values and a custom attribute StringValue for additional metadata.

  9. "C# enum with string values and serialization using JsonConverter"

    using System.Text.Json.Serialization; [JsonConverter(typeof(JsonStringEnumConverter))] public enum MyEnum { EnumValue1, EnumValue2, // Add more enum values } 

    Description: Defines an enum MyEnum with string values and specifies a JsonConverter for JSON serialization.


More Tags

android-video-player long-long screen-recording selector mkmapview soap-client elasticsearch go-ethereum android-studio-import space-analysis

More C# Questions

More Stoichiometry Calculators

More Electrochemistry Calculators

More Mortgage and Real Estate Calculators

More Weather Calculators