C# enum
Enumeration means the action of numbering elements one by one.
In C#, enumeration is done using the keyword enum
. C# enum defines a list of elements assigned with an ordered set of integer values.
C# enum is a value type datatype. It is used to store named integer constants.
The syntax to declare an enum is shown below:
enum EnumName{
CONSTANT_NAME_1,
CONSTANT_NAME_2,
CONSTANT_NAME_N
}
where EnumName
is the name given to this enum and with which we can reference.
CONSTANT_NAME_1
, CONSTANT_NAME_2
, . . etc., are the constants inside the enum.
Example 1 – C# enum
In the following example, we use enum named Operation
to store constants that represent mathematical operations.
This example demonstrates the declaration of Enum, initializing a variable with value of this Enum type, and then accessing the elements of this Enum.
Program.cs
using System;
namespace CSharpExamples {
enum Operation{
ADDITION,
SUBTRACTION,
MULTIPLICATION
}
class Program {
static void Main(string[] args) {
int a = 10;
int b = 4;
int result=0;
Operation op = Operation.ADDITION;
if(op==Operation.ADDITION){
result = a+b;
} else if(op==Operation.SUBTRACTION){
result = a-b;
} else if(op==Operation.MULTIPLICATION){
result = a*b;
}
Console.WriteLine("Result is : "+result);
}
}
}
Output
Result is : 14
Let us analyse this example.
To define an enum named Operations
, we used the following code snippet.
enum Operation{
ADDITION,
SUBTRACTION,
MULTIPLICATION
}
If we do not specify any value to the named constant, the first constant will take value of 0, and the rest would be incremented one by one.
So, here ADDITION = 0
, SUBTRACTION = 1
and MULTIPLICATION = 2
.
Inside the program, we defined a variable of type Operation and assigned a value to it.
Operation op = Operation.ADDITION;
Example – C# Enum – Print enum values
As we already mentioned, enum named constants are integers. In this example, we will print those enums and see their values.
Program.cs
using System;
namespace CSharpExamples {
enum Operation{
ADDITION,
SUBTRACTION,
MULTIPLICATION
}
class Program {
static void Main(string[] args) {
Console.WriteLine("Operation.ADDITION (without parsing to int) : "+Operation.ADDITION);
Console.WriteLine("Operation.ADDITION : "+(int)Operation.ADDITION);
Console.WriteLine("Operation.SUBTRACTION : "+(int)Operation.SUBTRACTION);
Console.WriteLine("Operation.MULTIPLICATION : "+(int)Operation.MULTIPLICATION);
}
}
}
Output
Operation.ADDITION (without parsing to int) : ADDITION
Operation.ADDITION : 0
Operation.SUBTRACTION : 1
Operation.MULTIPLICATION : 2
The value without parsing prints the named constant as is. If we type cast the enum constant to integer, we get their respective integer values.
Example – C# Enum – With values assigned to named constants
You can provide your own integer values to the named constants in the enum.
Program.cs
using System;
namespace CSharpExamples {
enum Operation{
ADDITION = 14,
SUBTRACTION = 17,
MULTIPLICATION = 27
}
class Program {
static void Main(string[] args) {
Console.WriteLine("Operation.ADDITION (without parsing to int) : "+Operation.ADDITION);
Console.WriteLine("Operation.ADDITION : "+(int)Operation.ADDITION);
Console.WriteLine("Operation.SUBTRACTION : "+(int)Operation.SUBTRACTION);
Console.WriteLine("Operation.MULTIPLICATION : "+(int)Operation.MULTIPLICATION);
}
}
}
Output
Operation.ADDITION (without parsing to int) : ADDITION
Operation.ADDITION : 14
Operation.SUBTRACTION : 17
Operation.MULTIPLICATION : 27
Example – C# Enum – With only first named constant assigned a value
If you assign a value only to the first constant, the rest of the constants will take a value incremented of its previous constant’s value.
Program.cs
using System;
namespace CSharpExamples {
enum Operation{
ADDITION = 34,
SUBTRACTION,
MULTIPLICATION
}
class Program {
static void Main(string[] args) {
Console.WriteLine("Operation.ADDITION : "+(int)Operation.ADDITION);
Console.WriteLine("Operation.SUBTRACTION : "+(int)Operation.SUBTRACTION);
Console.WriteLine("Operation.MULTIPLICATION : "+(int)Operation.MULTIPLICATION);
}
}
}
Output
Operation.ADDITION : 34
Operation.SUBTRACTION : 35
Operation.MULTIPLICATION : 36
Conclusion
In this C# Tutorial, we learned what an enum is in C#, and how to define an enum, access an enum, and other important points about C# enum with the help of examples.