How to get Description value of a Enum in C#

I have following code and would like to know how to get Description  value of a Enum from Program class


public enum Samplecolumns
    {
        [Description("one")]
        col1=1,
        [Description("two")]
        col2=2,
        [Description("three")]
        col3=3
    }


public class Sample
    {
          public Samplecolumns Column { get; set; }
    }


public class Program
    {
        static void Main(string[] args)
        {
                //How to get Description of enum ?
         
        }
    }

Open in new window


Thanks in Advance
ksd123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
int value = 1;
string description = Enumerations.GetEnumDescription((Samplecolumns)value);
0
Fernando SotoRetiredCommented:
Hi ksd123;

The following code snippet should work with your posted sample code.

// To get the description in this next line supply the enum field name as a string, in this case col3
// Also supply the name of the Enum to use here EnumHelper<XXX> replace XXX with that name.
string description = EnumHelper<Samplecolumns>.GetEnumDescription("col3");

public static class EnumHelper<T>
{
    public static string GetEnumDescription(string value)
    {
        Type type = typeof(T);
        var name = Enum.GetNames(type)
                       .Where(f => f.Equals(value, StringComparison.CurrentCultureIgnoreCase))
                       .Select(d => d).FirstOrDefault();
         
        if (name == null)
        {
            return string.Empty;
        }
        
        var field = type.GetField(name);
        var customAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
        
        return customAttribute.Length > 0 ? ((DescriptionAttribute)customAttribute[0]).Description : name;
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ksd123Author Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.