C# Enums
In this chapter you will learn:
- What
- How to use enum type
- Example for create an enum type
- Print out the details of any enum
- How to create enum type
- Is enum value defined
- How to use enum with switch statement
- How to get all names for an enum
- How to create enum from string
- How to check if a string value is defined
Description
An enum is a special value type that lets you specify a group of named numeric constants.
Syntax
The keyword enum
declares an enumerated type.
The general form for an enumeration is
enum name {
enumeration list
};
name
specifies the enumeration type name.
enumeration list is a comma-separated list of identifiers.
Each of the symbols stands for an integer value. Each of the symbols has a value one greater than the symbol that precedes it. By default, the value of the first enumeration symbol is 0.
Example
Example for create an enum type
using System;//from ww w .j av a2s. c o m
enum DaysOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
class MainClass
{
static void Main(string[] args)
{
DaysOfWeek Today = DaysOfWeek.Monday;
Console.WriteLine(Today);
}
}
The code above generates the following result.
Example 2
Print out the details of any enum
using System;//from ww w. j a v a 2s. co m
using System.Collections.Generic;
using System.Text;
enum EmpType : byte
{
Manager = 10,
Grunt = 1,
Contractor = 100,
VicePresident = 9
}
class Program
{
static void Main(string[] args)
{
EmpType e2 = EmpType.Contractor;
DayOfWeek day = DayOfWeek.Friday;
ConsoleColor cc = ConsoleColor.Black;
EvaluateEnum(e2);
EvaluateEnum(day);
EvaluateEnum(cc);
}
static void EvaluateEnum(System.Enum e)
{
Console.WriteLine("=> Information about {0}", e.GetType().Name);
Console.WriteLine("Underlying storage type: {0}",Enum.GetUnderlyingType(e.GetType()));
Array enumData = Enum.GetValues(e.GetType());
Console.WriteLine("This enum has {0} members.", enumData.Length);
for (int i = 0; i < enumData.Length; i++)
{
Console.WriteLine("Name: {0}, Value: {0:D}", enumData.GetValue(i));
}
}
}
The code above generates the following result.
Example 3
We can create enum type from existing enum type.
enum WeekEnd{//w w w . j a v a 2 s.c o m
Saturday, Sunday
}
enum WeekDay{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday = WeekEnd.Saturday, Sunday = WeekEnd.Sunday
}
Example 4
Enum has a static method we can use to check if an enum value is defined.
using System;//from ww w .ja va2 s . c om
public enum WeekDay{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
class Test
{
static void Main()
{
WeekDay day = (WeekDay)111;
if (Enum.IsDefined(day.GetType(),day))
{
Console.WriteLine("defined");
}
else
{
Console.WriteLine("not defined");
}
}
}
The output:
Example 5
How to use enum with switch statement
using System;/* w w w . j av a 2 s.co m*/
enum EmployeeType : byte
{
Manager = 10,
Programmer = 1,
Contractor = 100,
Developer = 9
}
class MainClass
{
public static void myFunction(EmployeeType e)
{
switch(e)
{
case EmployeeType.Contractor:
Console.WriteLine("EmployeeType.Contractor");
break;
case EmployeeType.Programmer:
Console.WriteLine("EmployeeType.Programmer");
break;
case EmployeeType.Manager:
Console.WriteLine("EmployeeType.Manager");
break;
case EmployeeType.Developer:
Console.WriteLine("EmployeeType.Developer");
break;
default: break;
}
}
public static void Main(string[] args)
{
EmployeeType fred;
fred = EmployeeType.Developer;
myFunction(fred);
}
}
The code above generates the following result.
Example 6
How to get all names for an enum
using System;/*from w w w . j a v a2s. c o m*/
enum Color
{
red,
green,
yellow
}
public class MainClass
{
public static void Main()
{
Color c = Color.red;
foreach (string s in Enum.GetNames(c.GetType()))
{
Console.WriteLine("Name: {0}", s);
}
}
}
The code above generates the following result.
Example 7
How to create enum from string
using System;//from w ww .j a v a2 s .c om
enum Color
{
red,
green,
yellow
}
public class MainClass
{
public static void Main()
{
Color c = Color.red;
//
c = (Color) Enum.Parse(typeof(Color), "Red", true);
Console.WriteLine("string value is: {0}", c);
}
}
The code above generates the following result.
Example 8
How to check if a string value is defined
using System;//ww w.j av a 2 s . co m
enum EmployeeType : byte
{
Manager = 10,
Programmer = 1,
Contractor = 100,
Developer = 9
}
class MainClass
{
public static void Main(string[] args)
{
if(Enum.IsDefined(typeof(EmployeeType), "SalesPerson"))
Console.WriteLine("Yep, we have sales people.");
else
Console.WriteLine("No, we have no profits....");
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Assign int value to enumerations
- Enumerations Initialization with calculation
- Output enum element value
- Loop through the enum data type
- Get all stats for an enum: Enum.GetValues()