Introduction
Enums are known as named constants. If we have some constants related to each other, then we can use an enum to group all the constants. Enums are strongly typed constants that make the code more readable and less prone to errors. It is useful when you have a set of values that are functionally significant and unchanged. An enumeration is a user-defined data type consisting of integral constants, and each integral constant is given a name. The keyword enum is used to define an enumerated data type.
Enum is a very easy and interesting topic. Today we learned some new things about enums.
Syntax
enum Enum_Name {
Value1,
Value2,
Value3,
// Add more values as needed...
ValueN
};
Example 1
using System;
enum Enum_Name
{
Sun,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Enum_Name.Sun);
Console.WriteLine(Enum_Name.Mon);
Console.WriteLine(Enum_Name.Tue);
Console.WriteLine(Enum_Name.Wed);
Console.WriteLine(Enum_Name.Thu);
Console.WriteLine(Enum_Name.Fri);
Console.WriteLine(Enum_Name.Sat);
Console.WriteLine((int)Enum_Name.Sun);
Console.WriteLine((int)Enum_Name.Mon);
Console.WriteLine((int)Enum_Name.Tue);
Console.WriteLine((int)Enum_Name.Wed);
Console.WriteLine((int)Enum_Name.Thu);
Console.WriteLine((int)Enum_Name.Fri);
Console.WriteLine((int)Enum_Name.Sat);
Console.ReadLine();
}
}
Output

This example illustrates how to access the name and value of a variable of an enum.
Example 2
using System;
enum Enum_Name
{
Sun,
Mon,
Tue,
Wed = 1000,
Thu,
Fri,
Sat
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine((int)Enum_Name.Sun);
Console.WriteLine((int)Enum_Name.Mon);
Console.WriteLine((int)Enum_Name.Tue);
Console.WriteLine((int)Enum_Name.Wed);
Console.WriteLine((int)Enum_Name.Thu);
Console.WriteLine((int)Enum_Name.Fri);
Console.WriteLine((int)Enum_Name.Sat);
Console.ReadLine();
}
}
Output

This example illustrates that if we assign an integer value to an enum variable, then the next variable will contain a value just greater than 1 from the previous value.
Example 3
using System;
enum Enum_Name
{
Sun,
Mon,
Tue,
Wed = 20000000000,
Thu,
Fri,
Sat
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine((int)Enum_Name.Sun);
Console.WriteLine((int)Enum_Name.Mon);
Console.WriteLine((int)Enum_Name.Tue);
Console.WriteLine((int)Enum_Name.Wed);
Console.WriteLine((int)Enum_Name.Thu);
Console.WriteLine((int)Enum_Name.Fri);
Console.WriteLine((int)Enum_Name.Sat);
Console.ReadLine();
}
}
Output

The preceding program will throw an error because, by default, the data type of an enum is an integer. In the preceding example, we exceed the range of integers, so it throws an error. To remove this error, we type-cast the enum.
using System;
enum Enum_Name : long
{
Sun,
Mon,
Tue,
Wed = 20000000000,
Thu,
Fri,
Sat
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine((int)Enum_Name.Sun);
Console.WriteLine((int)Enum_Name.Mon);
Console.WriteLine((int)Enum_Name.Tue);
Console.WriteLine((long)Enum_Name.Wed);
Console.WriteLine((long)Enum_Name.Thu);
Console.WriteLine((long)Enum_Name.Fri);
Console.WriteLine((long)Enum_Name.Sat);
Console.ReadLine();
}
}
Output

Example 4
using System;
enum Enum_Name : long
{
Sun = 25,
Mon,
Tue = Sun,
Wed = Sun,
Thu = Sun,
Fri = Sun,
Sat = Sun
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine((int)Enum_Name.Sun);
Console.WriteLine((int)Enum_Name.Mon);
Console.WriteLine((int)Enum_Name.Tue);
Console.WriteLine((long)Enum_Name.Wed);
Console.WriteLine((long)Enum_Name.Thu);
Console.WriteLine((long)Enum_Name.Fri);
Console.WriteLine((long)Enum_Name.Sat);
Console.ReadLine();
}
}
Output

Using an enum, we can assign a value to a variable from pre-assigned variables.
Example 5
using System;
enum Enum_Name : int
{
Sun,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat,
Sun // Duplicate value is allowed, but it will not affect the enumeration
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine((int)Enum_Name.Sun);
Console.WriteLine((int)Enum_Name.Mon);
Console.WriteLine((int)Enum_Name.Tue);
Console.WriteLine((int)Enum_Name.Wed);
Console.WriteLine((int)Enum_Name.Thu);
Console.WriteLine((int)Enum_Name.Fri);
Console.WriteLine((int)Enum_Name.Sat);
Console.ReadLine();
}
}
Output

In an enum, we cannot declare the same variable more than one time; each variable must be unique.
Example 6
using System;
enum Enum_Name
{
Sun,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat
}
class Program
{
static void Main(string[] args)
{
string[] Str = Enum_Name.GetNames(typeof(Enum_Name));
Array stm = Enum_Name.GetValues(typeof(Enum_Name));
foreach (string s in Str)
Console.WriteLine(s);
foreach (int num in stm)
Console.WriteLine(num);
Console.ReadLine();
}
}
Output

This example illustrates how to access a variable name and the values of an enum using a loop. This approach is very helpful when our enum contains a large number of variables.
Example 7
enum Enum_Name
{
Sun, Mon, Tue, Wed, Thu, Fri, Sat
}
static void Main(string[] args)
{
Console.WriteLine(Enum_Name.Sun + Enum_Name.Thu);
Console.ReadLine();
}
Output

C# doesn't allow us to do arithmetic operations on two members of an enum as shown in the preceding program. In order to do so, we need to overload the arithmetic operator.
enum Enum_Name
{
Sun, Mon, Tue, Wed, Thu, Fri, Sat
}
static void Main(string[] args)
{
Console.WriteLine(((int)Enum_Name.Mon) + ((int)Enum_Name.Thu));
Console.WriteLine(((int)Enum_Name.Mon) - ((int)Enum_Name.Thu));
Console.WriteLine(((int)Enum_Name.Mon) * ((int)Enum_Name.Thu));
Console.WriteLine(((int)Enum_Name.Mon) / ((int)Enum_Name.Thu));
Console.ReadLine();
}
Output

Example 8
enum Enum_Name1
{
Sun, Mon, Tue, Wed, Thu, Fri, Sat
}
enum Enum_Name2
{
Sun, Mon, Tue, Wed, Thu, Fri, Sat
}
static void Main(string[] args)
{
if (Enum_Name1.Sun == Enum_Name2.Sun)
{
Console.WriteLine("Both Are Same");
}
else
{
Console.WriteLine("Both Are Not Same");
}
Console.ReadLine();
}
Output














Pankaj Kumar ChoudharyPosted Feb 12, 2016, 1:39 AM
Thanks Shashank........
Pankaj Kumar ChoudharyPosted Feb 12, 2016, 1:38 AM
Thanks Arul R Sir.......
Pankaj Kumar ChoudharyPosted Feb 12, 2016, 1:38 AM
Thanks Amol Sir........
Pankaj Kumar ChoudharyPosted Feb 12, 2016, 1:38 AM
Thanks Nitin Tyagi Sir.....
Shashank MathPosted Feb 12, 2016, 1:08 AM
well explained buddy..!
Arul RPosted Jan 18, 2016, 6:58 AM
Nice share
Amol SarkatePosted Jun 9, 2015, 2:01 AM
Nice Explanation ......!
NitinPosted Jun 1, 2015, 9:50 AM
good one
RakeshPosted May 31, 2015, 1:09 AM
Good explain
Abhishek JaiswalPosted May 31, 2015, 12:20 AM
Very well explained buddy, especially the examples. You tried to keep it as simple as you can, that's good. Keep it up!! :)
Sam HobbsPosted May 30, 2015, 11:03 PM
Flag enums are also useful when working with other languages and with the operating system. It is vey common for an operating system and other software to store parameters and other data as bit values.