C# Enum
Definition
In C#, Enum is a value type whose members are named constant and the underlying value is an integral numeric type.
C# supports below integral types:
- sbyte
- byte
- short
- ushort
- int
- uint
- long
- ulong
int
is the default type of enum underlying value.
Syntax
Enum
keyword must be used to define enum followed by enum’s name and its members:
enum Rank
{
Lieutenant,
Captain,
Major,
Colonel,
General
}
By default, each member represent an integer starts from 0, so above enum is implicitly the same with below enum.
enum Rank
{
Lieutenant = 0,
Captain = 1,
Major = 2,
Colonel = 3,
General = 4
}
The value can also be assigned differently
enum Rank
{
Lieutenant = 0,
Captain = 15,
Major = 217,
Colonel = 3200,
General = 4125
}
You can also explicitly set the underlying type to other integral numeric type
enum Rank : byte
{
Lieutenant = 0,
Captain = 1,
Major = 2,
Colonel = 3,
General = 255
}
If you set value outside the range of the integral type then you will get compile error.
C# Enum Conversion
Enum can be converted to its underlying value and the other way around through explicit conversion.
class App
{
enum Rank : byte
{
Lieutenant = 0,
Captain = 1,
Major = 2,
Colonel = 3,
General = 255
}
public static void Main(string[] args)
{
var rankValue = (int)Rank.General;
Console.WriteLine(rankValue);
var rank = (Rank)3;
Console.WriteLine(rank);
var a = (Rank)5;
Console.WriteLine(a);
}
}
Output:
255
Colonel
5
C# Iterate through Enum
There are two ways to iterate an enum:
- Enum.GetValues(typeof(your-enum))
- Enum.GetNames(typeof(your-enum))
The difference between those two methods are GetValues
return Array
object while GetNames
return array of string (member name).
class App
{
enum Rank
{
Lieutenant = 0,
Captain = 1,
Major = 2,
Colonel = 3,
General = 255
}
public static void Main(string[] args)
{
var values = Enum.GetValues(typeof(Rank));
foreach (var value in values)
{
Console.WriteLine(value);
}
Console.WriteLine();
string[] strNames = Enum.GetNames(typeof(Rank));
foreach (string name in strNames)
{
Console.WriteLine(name);
}
}
}
Output:
Lieutenant
Captain
Major
Colonel
General
Lieutenant
Captain
Major
Colonel
General
C# Get Enum Description
Enum doesn’t allow method inside it, but you can extend its functionality by creating extension method.
In below example, you add description attribute to each member and use GetDescription
method to get the description.
using System;
using System.ComponentModel;
using System.Reflection;
namespace EnumDemo
{
public class App
{
public enum Rank
{
[Description("Fifth Rank")]
Lieutenant,
[Description("Fourth Rank")]
Captain,
[Description("Third Rank")]
Major,
[Description("Second Rank")]
Colonel,
[Description("First Rank")]
General
}
public static void Main(string[] args)
{
string description = Rank.General.GetDescription();
Console.WriteLine(description);
}
}
public static class MyExtensions
{
public static string GetDescription(this Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString());
DescriptionAttribute attribute
= Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
}
}
Output:
First Rank