C# Enum Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To get the description attributes of enum values in C#, you can define a custom attribute, such as DescriptionAttribute
, on each enum value and then use reflection to retrieve the attribute values.
Here's an example:
using System; using System.ComponentModel; public enum MyEnum { [Description("First value")] Value1, [Description("Second value")] Value2, [Description("Third value")] Value3 } public static class EnumHelper { public static string GetDescription(Enum value) { var field = value.GetType().GetField(value.ToString()); if (field == null) return null; var attributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length == 0) return null; return ((DescriptionAttribute)attributes[0]).Description; } } // Usage: var myEnumValue = MyEnum.Value1; var description = EnumHelper.GetDescription(myEnumValue); // "First value"
To get the value of the EnumMember
attribute in C#, you can use reflection to retrieve the attribute values.
Here's an example:
using System; using System.Runtime.Serialization; [DataContract] public enum MyEnum { [EnumMember(Value = "First value")] Value1, [EnumMember(Value = "Second value")] Value2, [EnumMember(Value = "Third value")] Value3 } public static class EnumHelper { public static string GetEnumMemberValue(Enum value) { var field = value.GetType().GetField(value.ToString()); if (field == null) return null; var attributes = field.GetCustomAttributes(typeof(EnumMemberAttribute), false); if (attributes.Length == 0) return null; return ((EnumMemberAttribute)attributes[0]).Value; } } // Usage: var myEnumValue = MyEnum.Value1; var value = EnumHelper.GetEnumMemberValue(myEnumValue); // "First value"
To get the display name attribute of an enum member in C#, you can define a custom attribute, such as DisplayNameAttribute
, on each enum value and then use reflection to retrieve the attribute values.
Here's an example:
using System; using System.ComponentModel; public enum MyEnum { [DisplayName("First value")] Value1, [DisplayName("Second value")] Value2, [DisplayName("Third value")] Value3 } public static class EnumHelper { public static string GetDisplayName(Enum value) { var field = value.GetType().GetField(value.ToString()); if (field == null) return null; var attributes = field.GetCustomAttributes(typeof(DisplayNameAttribute), false); if (attributes.Length == 0) return null; return ((DisplayNameAttribute)attributes[0]).DisplayName; } } // Usage: var myEnumValue = MyEnum.Value1; var displayName = EnumHelper.GetDisplayName(myEnumValue); // "First value"
To get custom attributes of enum values in C#, you can use reflection to loop through each enum value and get its custom attributes. Here is an example:
using System; using System.Linq; public class Program { public static void Main() { var values = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>(); foreach (var value in values) { var memberInfo = typeof(MyEnum).GetMember(value.ToString()).FirstOrDefault(); if (memberInfo != null) { var customAttributes = memberInfo.GetCustomAttributes(false); foreach (var attribute in customAttributes) { Console.WriteLine($"{value} - {attribute.GetType().Name}"); } } } } } public enum MyEnum { [MyCustom("Value 1")] Value1, [MyCustom("Value 2")] Value2, Value3 } public class MyCustomAttribute : Attribute { public MyCustomAttribute(string value) { Value = value; } public string Value { get; } }
In this example, we define a custom attribute MyCustomAttribute
and apply it to some of the enum values in MyEnum
. We then use reflection to loop through each enum value, get its custom attributes, and print out the attribute type and value.
To check if an attribute exists on an enum element, you can use the Attribute.IsDefined
method. Here is an example:
var value = MyEnum.Value1; if (Attribute.IsDefined(typeof(MyEnum).GetField(value.ToString()), typeof(MyCustomAttribute))) { Console.WriteLine($"{value} has MyCustomAttribute"); }
In this example, we check if the MyCustomAttribute
is defined on the Value1
enum element of MyEnum
. If it is defined, we print out a message indicating that it has the attribute.
C# get attribute values from enum: Retrieving attribute values from an enum in C#.
DaysOfWeek day = DaysOfWeek.Monday; var descriptionAttribute = GetEnumAttribute<DescriptionAttribute>(day); string description = descriptionAttribute?.Description;
Retrieve custom attributes of enum in C#: Retrieving custom attributes from an enum in C#.
DaysOfWeek day = DaysOfWeek.Monday; var customAttributes = GetEnumCustomAttributes(day);
Accessing enum attributes using Reflection in C#: Accessing enum attributes using Reflection in C#.
DaysOfWeek day = DaysOfWeek.Monday; var attributes = day.GetType().GetField(day.ToString()).GetCustomAttributes(false);
Using Attribute.GetCustomAttribute with enum in C#:
Using Attribute.GetCustomAttribute
to retrieve an attribute from an enum.
DaysOfWeek day = DaysOfWeek.Monday; var descriptionAttribute = Attribute.GetCustomAttribute(day.GetType().GetField(day.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute; string description = descriptionAttribute?.Description;
Get description attribute value from enum in C#:
Retrieving the value of the Description
attribute from an enum.
DaysOfWeek day = DaysOfWeek.Monday; string description = GetEnumDescription(day);
Custom attribute values for enum members in C#: Creating and accessing custom attribute values for enum members.
public class CustomAttribute : Attribute { public string CustomValue { get; } public CustomAttribute(string customValue) { CustomValue = customValue; } } public enum CustomEnum { [Custom("Value1")] Option1, [Custom("Value2")] Option2 } var customValue = GetEnumAttribute<CustomAttribute>(CustomEnum.Option1)?.CustomValue;
Accessing metadata from enum attributes in C#: Accessing metadata from enum attributes.
DaysOfWeek day = DaysOfWeek.Monday; var attribute = GetEnumAttribute<CustomAttribute>(day); string metadata = attribute?.CustomValue;
Retrieve enum values based on custom attributes in C#: Retrieving enum values based on custom attributes.
var matchingDays = GetEnumValuesWithAttribute<CustomAttribute>("Value1");
C# get all attributes of an enum member: Getting all attributes of an enum member in C#.
DaysOfWeek day = DaysOfWeek.Monday; var attributes = GetEnumAttributes(day);
Using Enum.GetValues and reflection to access enum attributes in C#:
Using Enum.GetValues
and reflection to access enum attributes.
foreach (DaysOfWeek day in Enum.GetValues(typeof(DaysOfWeek))) { var descriptionAttribute = GetEnumAttribute<DescriptionAttribute>(day); string description = descriptionAttribute?.Description; }