Enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.
C# enumerations are value data type. In other words, enumeration contains its own values and cannot inherit or cannot pass inheritance.
Declaring enum Variable
The general syntax for declaring an enumeration is −
enum <enum_name>
{
enumeration list
}
Example Code:-
The following example demonstrates use of enum variable −
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Enumerators
{
class Program
{
enum MyColors
{
Red=20,Green=30,Blue=40
}
static void Main(string[] args)
{
string print = "welcome";
Console.WriteLine("1:Red");
Console.WriteLine("2:Green");
Console.WriteLine("3:Blue");
Console.WriteLine("Enter ur choice");
int ch = int.Parse(Console.ReadLine());
if (ch == (int)MyColors.Red)
Console.BackgroundColor = ConsoleColor.Red;
else if (ch == (int)MyColors.Green)
Console.BackgroundColor = ConsoleColor.Green;
else if (ch == (int)MyColors.Blue)
Console.BackgroundColor = ConsoleColor.Blue;
else
print = "Invalid Choice";
Console.WriteLine(print);
Console.ReadKey();
}
}
}
Comments
Post a Comment