HOW TO USE ENUM IN C#
Create Console
Application => In Class program
declare Enum
internal enum MicrosoftTechnology
{
SharePoint = 1,
ASPNET = 2,
CSharp = 3,
SQL
= 4,
JQuery = 5
}
Get Enum name
Code
private static void GetEnumName()
{
int[] enumValues = new int[] { 1, 2, 3, 4, 5 };
foreach (int enumValue in enumValues)
{
Console.WriteLine(Enum.GetName(typeof(MicrosoftTechnology), enumValue));
}
}
Result
Get array of Enum
Code
private static void IterateEnumVariables()
{
Console.WriteLine("Lists of Enums: ");
string[] technicalNames = Enum.GetNames(typeof(MicrosoftTechnology));
foreach (string technicalName in technicalNames)
{
Console.WriteLine(technicalName);
}
}
Result
Get Enum values
Code
private static void GetEnumValues()
{
Array enumArray = Enum.GetValues(typeof(MicrosoftTechnology));
foreach (int enumValue in enumArray)
{
Console.WriteLine("Value: " + enumValue + " - Name: " + Enum.GetName(typeof(MicrosoftTechnology), enumValue));
}
}
Result
Convert Enum to string
Code
private static void EnumToString()
{
Console.WriteLine("Convert Enum ToString():
");
Console.WriteLine(MicrosoftTechnology.ASPNET.ToString());
Console.WriteLine(MicrosoftTechnology.SharePoint.ToString());
}
Result
Parse Enum
Code
private static void ParseEnum()
{
MicrosoftTechnology ms = (MicrosoftTechnology)Enum.Parse(typeof(MicrosoftTechnology), "ASPNET");
Console.WriteLine(ms.ToString());
MicrosoftTechnology ms1 = (MicrosoftTechnology)Enum.Parse(typeof(MicrosoftTechnology), "SharePoint");
Console.WriteLine(ms1.ToString());
Console.ReadKey();
}
Result
TryParse Enum
Code
private static void TryParseEnum()
{
string a = (MicrosoftTechnology.JQuery).ToString();
MicrosoftTechnology b;
bool success = Enum.TryParse<MicrosoftTechnology>(a, out b);
Console.WriteLine("{0} = {1}", success, b);
}
Result
TryParse Enum
Code
private static void TryParseEnum1()
{
MicrosoftTechnology msDefault = MicrosoftTechnology.SharePoint;
//Supplying correct enum string.
bool ms = Enum.TryParse("ASPNET", true, out msDefault);
Console.WriteLine(ms.ToString() + " " + msDefault.ToString());
MicrosoftTechnology msDefault1 = MicrosoftTechnology.JQuery;
//Supplying wrong enum string.
bool ms1 = Enum.TryParse("JQuer", out msDefault1);
Console.WriteLine(ms1.ToString() + " " + msDefault1.ToString());
Console.ReadKey();
}
Result
All Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CShrapEnum
{
class Program
{
internal enum MicrosoftTechnology
{
SharePoint = 1,
ASPNET = 2,
CSharp = 3,
SQL
= 4,
JQuery = 5
}
private static void GetEnumName()
{
int[] enumValues = new int[] { 1, 2, 3, 4, 5 };
foreach (int enumValue in enumValues)
{
Console.WriteLine(Enum.GetName(typeof(MicrosoftTechnology), enumValue));
}
}
private static void IterateEnumVariables()
{
Console.WriteLine("Lists of Enums: ");
string[] technicalNames = Enum.GetNames(typeof(MicrosoftTechnology));
foreach (string technicalName in technicalNames)
{
Console.WriteLine(technicalName);
}
}
private static void GetEnumValues()
{
Array enumArray = Enum.GetValues(typeof(MicrosoftTechnology));
foreach (int enumValue in enumArray)
{
Console.WriteLine("Value: " + enumValue + " - Name: " + Enum.GetName(typeof(MicrosoftTechnology), enumValue));
}
}
private static void EnumToString()
{
Console.WriteLine("Convert Enum ToString():
");
Console.WriteLine(MicrosoftTechnology.ASPNET.ToString());
Console.WriteLine(MicrosoftTechnology.SharePoint.ToString());
}
private static void ParseEnum()
{
MicrosoftTechnology ms = (MicrosoftTechnology)Enum.Parse(typeof(MicrosoftTechnology), "ASPNET");
Console.WriteLine(ms.ToString());
MicrosoftTechnology ms1 = (MicrosoftTechnology)Enum.Parse(typeof(MicrosoftTechnology), "SharePoint");
Console.WriteLine(ms1.ToString());
Console.ReadKey();
}
private static void TryParseEnum()
{
string a = (MicrosoftTechnology.JQuery).ToString();
MicrosoftTechnology b;
bool success = Enum.TryParse<MicrosoftTechnology>(a, out b);
Console.WriteLine("{0} = {1}", success, b);
}
private static void TryParseEnum1()
{
MicrosoftTechnology msDefault = MicrosoftTechnology.SharePoint;
//Supplying correct enum string.
bool ms = Enum.TryParse("ASPNET", true, out msDefault);
Console.WriteLine(ms.ToString() + " " + msDefault.ToString());
MicrosoftTechnology msDefault1 = MicrosoftTechnology.JQuery;
//Supplying wrong enum string.
bool ms1 = Enum.TryParse("JQuer", out msDefault1);
Console.WriteLine(ms1.ToString() + " " + msDefault1.ToString());
Console.ReadKey();
}
static void Main(string[] args)
{
//GetEnumName();
//IterateEnumVariables();
//GetEnumValues();
//EnumToString();
//ParseEnum();
//TryParseEnum();
TryParseEnum1();
Console.ReadLine();
}
}
}
Done!!!