Cinchoo – String.ToEnum(), Extension Method

In this section, I’ll talk about ToEnum<T> extension method. This method used get enum value from enum description.

1. Add reference to Cinchoo.Core.ExtensionMethods.dll assembly

2. Namespace System


public enum Color
    [Description("Red Color")]
    [Description("Green Color")]
    [Description("Yellow Color")]

static void Main(string[] args)
    Console.Write("Enum value for 'RED Color' is: ");
    Console.WriteLine("RED Color".ToEnum<Color>());

When you run the above code, the output will be

Enum value for 'RED Color' is: Red
Press any key to continue . . .

Happy coding!!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s