C# Enum Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
In C#, enums can only have underlying integral types, like byte
, sbyte
, short
, ushort
, int
, uint
, long
, or ulong
. You cannot directly define an enum with a string value. However, you can use a workaround to associate string values with enum members by using attributes.
Here's an example using a custom attribute to associate a string value with each enum member:
StringValueAttribute
:using System; public class StringValueAttribute : Attribute { public string StringValue { get; protected set; } public StringValueAttribute(string value) { StringValue = value; } }
StringValueAttribute
applied to each member:public enum Color { [StringValue("Red Color")] Red, [StringValue("Green Color")] Green, [StringValue("Blue Color")] Blue }
public static class EnumExtensions { public static string GetString(this Enum value) { var fieldInfo = value.GetType().GetField(value.ToString()); var attribute = (StringValueAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(StringValueAttribute)); return attribute == null ? value.ToString() : attribute.StringValue; } }
Now you can use the GetString
extension method to get the associated string value for an enum member:
using System; class Program { static void Main(string[] args) { Color color = Color.Red; string stringValue = color.GetString(); Console.WriteLine(stringValue); // Output: Red Color } }
In this example, the Color
enum has three members, and each member has an associated string value using the StringValueAttribute
. The EnumExtensions.GetString
method retrieves the string value of the enum member by looking up the StringValueAttribute
for the given enum member.
C# enum with string values example: Creating an enum with string values in C#.
public enum DaysOfWeek { Sunday = "Sun", Monday = "Mon", Tuesday = "Tue", Wednesday = "Wed", Thursday = "Thu", Friday = "Fri", Saturday = "Sat" }
String values in C# enum declaration: Declaring enum members with string values.
public enum DaysOfWeek { Sunday = "Sun", Monday = "Mon", Tuesday = "Tue", Wednesday = "Wed", Thursday = "Thu", Friday = "Fri", Saturday = "Sat" }
Assigning string values to enum members in C#: Assigning string values directly to enum members.
public enum DaysOfWeek { Sunday = "Sun", Monday = "Mon", Tuesday = "Tue", Wednesday = "Wed", Thursday = "Thu", Friday = "Fri", Saturday = "Sat" }
How to create a string-based enum in C#: Creating an enum with string values for representation.
public enum Colors { Red = "FF0000", Green = "00FF00", Blue = "0000FF" }
Enum with custom string representation in C#: Defining enums with custom string representations.
public enum Status { [StringValue("Active")] Active, [StringValue("Inactive")] Inactive }
Using DescriptionAttribute with enum in C#:
Utilizing DescriptionAttribute
to provide custom descriptions for enum values.
public enum Status { [Description("Active Status")] Active, [Description("Inactive Status")] Inactive }
C# enum with names and string values: Enum with both names and string values.
public enum Colors { Red = "FF0000", Green = "00FF00", Blue = "0000FF" }
Mapping enum values to strings in C#: Creating a mapping between enum values and strings.
public static class StatusMapper { public static string MapToString(Status status) { // Logic to map enum to string } }
String representation of enum values in C#: Retrieving the string representation of enum values.
Status status = Status.Active; string statusString = status.ToString(); // Converts enum to string
Enum extension methods for string values in C#: Adding extension methods for working with enum string values.
public static class EnumExtensions { public static string GetStringRepresentation(this Status status) { // Logic to retrieve string representation } }