C#

String Value Enum with Space in between – Description Attribute

Enum with spaces C#: If you give a space in string value of Enum, it would generate a compilation error. Single-word enum string value is a normal scenario and straightforward handle. But, how to have an enum string value with multiple words

Enum values with spaces in C#

Normally, the name of an enum in c# can’t contain any special characters or spaces. There can be situations where we need to use friendly names for enums which have spaces in between.

For example, if you have a string value enum as below

public enum Country
{
  India,
  United Arab Emirates,
  United Kingdom
}

You will get syntax error since this is not the correct way of enum declaration. If you remove spaces in between string value, it will become a valid enum.

But there may be cases still wherein you want string value enums with space between words. In that case, How to have enum values with spaces in C#?

You need to use Description attribute in this case. See the corrected enum snippet as below.

Associating Strings with enums in C#

In order to have a more friendly enum ,we can use System.ComponentModel.DescriptionAttribute. see the sample below,

public enum Country
{
    India,

    [Description("United Arab Emirates")]
    UAE,

    [Description("United Kingdom")]
    UK
}

As above, by decorating with [Description] attribute you can have a valid enum for your case.

How to Get the Description  Attribute Values from  Enum?

Now, how to fetch the Description attribute values instead of enum values. Means, you need United Arab Emirates instead of UAE.

You can use any of the method below for getting the Description attribute value from the corresponding enum value by using reflection.

public static string GetDescriptionFromEnum(Enum value)
{
     DescriptionAttribute attribute = value.GetType()
     .GetField(value.ToString())
     .GetCustomAttributes(typeof(DescriptionAttribute), false)
     .SingleOrDefault() as DescriptionAttribute;
     return attribute == null ? value.ToString() : attribute.Description;
}
public static string GetDescriptionFromEnum(Enum value)
{
      FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
      DescriptionAttribute[] attributes =
        (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
      return attributes == null && attributes.Length == 0 ? value.ToString() : attributes[0].Description;
}

Summary

This article covered Enum with spaces C#. You need to use C# Enum Attribute, named Description to have an enum string value with multiple words. If you find this article useful share your feedback in the comments section.

Rajeev

Recent Posts

OWIN Authentication in .NET Core

OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…

2 years ago

Serializing and Deserializing JSON using Jsonconvertor in C#

JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…

2 years ago

What is CAP Theorem? | What is Brewer’s Theorem?

The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…

2 years ago

SOLID -Basic Software Design Principles

Some of the Key factors that need to consider while architecting or designing a software…

2 years ago

What is Interface Segregation Principle (ISP) in SOLID Design Principles?

The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…

2 years ago

What is Single Responsibility Principle (SRP) in SOLID Design Priciples?

The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…

2 years ago