Friday, August 20, 2010

Enums in C#

Enums in C#


Enums are basically a set of named constants. i.e. these are basically a group of named constants defined under a common name.
Enums are value types and are created on the stack and not on the heap.

Eg:

using System;
public enum Colors
{
red,
green,
blue,
}


public class Mainclass
{
public static void Main()
{
string color = Colors.red.ToString();
Console.WriteLine("Color is:" + color);
int id = (int)Colors.blue;
Console.WriteLine("Color value:" + id);
}
}


Each enum type has a corresponding value type called underlying type of the enum type.

No comments: