In this article, you will see use of enum type variables in C#.
Sample enum variables to use:
- internal enum MicrosoftOffice
- {
- Word = 0,
- Excel = 1,
- Powerpoint = 3,
- Visio = 5
- }
How to get enum variable name by its value in C#?
- /// This is used to get Enum name by its value as integer.
- private static void GetEnumName()
- {
- int[] enumValues = new int[] { 0, 1, 3, 5 };
- foreach (int enumValue in enumValues)
- {
- Console.WriteLine(Enum.GetName(typeof(MicrosoftOffice), enumValue));
- }
- Console.ReadKey();
- }

How to iterate an enum variable or enum name in C#?
Retrieves an array of the names of the constants in a specified enumeration.
- /// This is used to iterate enum variables.
- private static void IterateEnumVariables()
- {
- Console.WriteLine("List of Enums");
- string[] officenames = Enum.GetNames(typeof(MicrosoftOffice));
- foreach (string officename in officenames)
- {
- Console.WriteLine(officename);
- }
- Console.ReadKey();
- }

How to get enum variable name by its value in C#?
Retrieves an array of the values of the constants in a specified enumeration.
- ///This is used to get Enum values as array.
- private static void GetEnumValues()
- {
- Array enumValueArray = Enum.GetValues(typeof(MicrosoftOffice));
- foreach (int enumValue in enumValueArray)
- {
- Console.WriteLine("Name: " + Enum.GetName(typeof(MicrosoftOffice), enumValue) + " , Value: " + enumValue);
- }
- Console.ReadKey();
- }

How to convert an enum to a String in C#?
ToString() converts Enum to string.
- /// This is used to convert Enum to string.
- private static void EnumToString()
- {
- Console.WriteLine("Converting Enum to String");
- Console.WriteLine(MicrosoftOffice.Powerpoint.ToString());
- Console.WriteLine(MicrosoftOffice.Word.ToString())
- Console.ReadKey();
- }

How to convert a String to an enum or Parse in C#?
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
- /// <summary>
- /// This is used to convert string to Enum variable.
- /// </summary>
- private static void ParseEnum()
- {
- MicrosoftOffice ms = (MicrosoftOffice)Enum.Parse(typeof(MicrosoftOffice), "Word");
- Console.WriteLine(ms.ToString());
- MicrosoftOffice ms1 = (MicrosoftOffice)Enum.Parse(typeof(MicrosoftOffice), "Excel");
- Console.WriteLine(ms1.ToString());
- Console.ReadKey();
- }

How to check and Parse a String from the enum variable using C#?
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. The return value indicates whether the conversion succeeded.
It is optionally case sensitive; you can just specify true or false to specify the option.
- /// <summary>
- /// This is used to check and get value of specified string.
- /// </summary>
- private static void TryParseEnum()
- {
- MicrosoftOffice msDefault = MicrosoftOffice.Powerpoint;
- //Supplying correct enum string.
- bool ms = Enum.TryParse("Word", true, out msDefault);
- Console.WriteLine(ms.ToString() + " " + msDefault.ToString());
- MicrosoftOffice msDefault1 = MicrosoftOffice.Powerpoint;
- //Supplying wrong enum string.
- bool ms1 = Enum.TryParse("excl", out msDefault1);
- Console.WriteLine(ms1.ToString() + " " + msDefault1.ToString());
- Console.ReadKey();
- }

How to check whether the passed enum exists or not using C#?
Indicates whether a constant with a specified value exists in a specified enumeration.
- /// This is used to check whether the enum is available in Enum type.
- private static void EnumIsDefined()
- {
- if (Enum.IsDefined(typeof(MicrosoftOffice), "Word"))
- {
- Console.WriteLine("Word Enum Exists");
- }
- if (Enum.IsDefined(typeof(MicrosoftOffice), "Excl"))
- {
- Console.WriteLine("Excel Enum Exists");
- }
- else
- {
- Console.WriteLine("Enum Not Exists");
- }
- Console.ReadKey();
- }

How to create a Custom String value from an enum variable and how to use it?
Here we can use any custom string to the enum variable so that it is very handy to use this custom string.
Steps to follow
- First create a sample set of enum variables.
- Then, create a custom attribute class for just custom String Value.
- Create an extension method for just enum.
- Use the extension method to get just enum custom string value.
1. Enum variable for custom String value in C#:
- internal enum MicrosoftOfficeString : int
- {
- [EnumStringAttribute("Microsoft Word")]
- Word = 0,
- [EnumStringAttribute("Microsoft Excel")]
- Excel = 1,
- [EnumStringAttribute("Microsoft Powerpoint")]
- Powerpoint = 3,
- [EnumStringAttribute("Microsoft Visio")]
- Visio = 5
- }
- public class EnumStringAttribute : Attribute
- {
- public EnumStringAttribute(string stringValue)
- {
- this.stringValue = stringValue;
- }
- private string stringValue;
- public string StringValue
- {
- get { return stringValue; }
- set { stringValue = value; }
- }
- }
- public static class ExtenstionClass
- {
- public static string GetStringValue(this Enum value)
- {
- Type type = value.GetType();
- FieldInfo fieldInfo = type.GetField(value.ToString());
- // Get the stringvalue attributes
- EnumStringAttribute[] attribs = fieldInfo.GetCustomAttributes(
- typeof(EnumStringAttribute), false) as EnumStringAttribute[];
- // Return the first if there was a match.
- return attribs.Length > 0 ? attribs[0].StringValue : null;
- }
- }
- /// This is used to get the Cutom Attribute.
- /// </summary>
- private static void GetEnumCustomAttributeString()
- {
- //This return the custom attribute string value.
- Console.WriteLine(MicrosoftOfficeString.Excel.GetStringValue());
- Console.WriteLine(Environment.NewLine);
- string[] officenames = Enum.GetNames(typeof(MicrosoftOffice));
- foreach (string officename in officenames)
- {
- MicrosoftOfficeString ms = (MicrosoftOfficeString)Enum.Parse(typeof(MicrosoftOfficeString), officename);
- //This return the custom attribute string value.
- Console.WriteLine(ms.GetStringValue());
- }
- Console.ReadKey();
- }


Cipher CoderPosted Jun 11, 2020, 2:00 AM
Good one ...
Vihaan RPosted Dec 8, 2019, 7:17 AM
Hi thank you for sharing! Very informative tutorial. If you would like to see more I encourage you to view as it is helpful. Thanks! You can check it out here: https://systemoutofmemory.com/blogs/the-programmer-blog/c-sharp-cast-int-to-enum
Rajeesh MenothPosted Jul 31, 2015, 1:08 AM
Good one
Mohamed YunusPosted Jul 6, 2015, 2:01 AM
Well try Arun
nikhil sreeniPosted Jan 30, 2013, 2:18 AM
Thanks for the post,Its really helpful
Nagashyam KumarPosted Mar 18, 2012, 7:39 AM
Good One
Amit MaheshwariPosted Mar 18, 2012, 12:32 AM
It's a very well explanation about the enum keep it up. But I want to know that where we can use it please tell me? And why we used it?