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#?
Retrieves the name of the constant in the specified enumeration that has the specified value.
-
-
- 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();
- }
Output
How to iterate an enum variable or enum name in C#?
Retrieves an array of the names of the constants in a specified enumeration.
-
- 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();
- }
Output
How to get enum variable name by its value in C#?
Retrieves an array of the values of the constants in a specified enumeration.
-
-
- 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();
- }
Output
How to convert an enum to a String in C#?
ToString() converts 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();
- }
Output
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.
-
-
-
- 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();
- }
Output
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.
-
-
-
- private static void TryParseEnum()
- {
- MicrosoftOffice msDefault = MicrosoftOffice.Powerpoint;
-
- bool ms = Enum.TryParse("Word", true, out msDefault);
- Console.WriteLine(ms.ToString() + " " + msDefault.ToString());
- MicrosoftOffice msDefault1 = MicrosoftOffice.Powerpoint;
-
- bool ms1 = Enum.TryParse("excl", out msDefault1);
- Console.WriteLine(ms1.ToString() + " " + msDefault1.ToString());
- Console.ReadKey();
- }
Output
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.
-
-
- 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();
- }
Output
How to create a Custom String value from an enum variable and how to use it?
This is used to set the Custom string to the enum variable as an attribute and get the value at runtime.
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
- }
2. How to create a custom attribute for a custom string value in C#?
- public class EnumStringAttribute : Attribute
- {
- public EnumStringAttribute(string stringValue)
- {
- this.stringValue = stringValue;
- }
- private string stringValue;
- public string StringValue
- {
- get { return stringValue; }
- set { stringValue = value; }
- }
- }
3.
How to create an extension method for an enum custom String?
- public static class ExtenstionClass
- {
- public static string GetStringValue(this Enum value)
- {
- Type type = value.GetType();
- FieldInfo fieldInfo = type.GetField(value.ToString());
-
- EnumStringAttribute[] attribs = fieldInfo.GetCustomAttributes(
- typeof(EnumStringAttribute), false) as EnumStringAttribute[];
-
- return attribs.Length > 0 ? attribs[0].StringValue : null;
- }
- }
4.
How to get a custom String value from an enum variable using attribute in C#?
-
-
- private static void GetEnumCustomAttributeString()
- {
-
- 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);
-
- Console.WriteLine(ms.GetStringValue());
- }
- Console.ReadKey();
- }
Output