Skip to main content

How to Declare an Enum in C#

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

An enum gives a fixed set of choices names, such as Espresso, Latte and Mocha, so your code says Coffee.Latte instead of a bare number. You declare one with the enum keyword, a name and a list of members.

Microsoft's reference: An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type. The C# guide says when to use one: Use enums when a value must be one of a fixed set of options, such as days of the week, HTTP status codes, log levels, or directions.

Syntax​

Syntax
enum <Name>
{
<Member1>,
<Member2>,
<Member3>,
}

<Name> <variable> = <Name>.<Member>;
PartWhat it is
enumDeclares a new type whose values are the members listed after it
<Name>The type's name, such as Coffee
<Member1>, <Member2>, <Member3>The members, separated by commas. The comma after the last one is optional
<Name>.<Member>One of the values: the type's name, a dot and the member's name

Microsoft's naming guidelines: Names of enumeration types (also called enums) in general should follow the standard type-naming rules (PascalCasing, etc.). They add: DO use a singular type name for an enumeration unless its values are bit fields. So Coffee, not Coffees or CoffeeEnum.

The numbers come on their own. The reference: By default, the associated constant values of enum members are of type int. They start with zero and increase by one following the definition text order.

The Example​

A console app called EnumDeclare declares a Coffee enum with three members, stores one of them in a variable, and prints the number behind each member.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n EnumDeclare
cd EnumDeclare
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/EnumDeclare/EnumDeclare.csproj:
Determining projects to restore...
Restored /Users/you/source/EnumDeclare/EnumDeclare.csproj (in 39 ms).
Restore succeeded.

dotnet new console -n EnumDeclare creates a folder named EnumDeclare with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or EnumDeclare.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the code that uses the enum:

Program.cs (the code that runs)
Coffee order = Coffee.Latte;

Console.WriteLine($"You ordered: {order}");

// Each member stands for a number: 0, 1 and 2.
Console.WriteLine($"Espresso is number {(int)Coffee.Espresso}");
Console.WriteLine($"Latte is number {(int)Coffee.Latte}");
Console.WriteLine($"Mocha is number {(int)Coffee.Mocha}");
  • Coffee order = Coffee.Latte; declares a variable of type Coffee and gives it the member Latte.
  • (int)Coffee.Espresso converts a member to int, which shows the number behind it.

Then the enum, at the end of the file:

Program.cs (the enum)
enum Coffee
{
Espresso,
Latte,
Mocha,
}

enum Coffee names the new type, and the three members follow, one per line with a comma after each. In a Program.cs like this one, where the code runs without a Main method, an enum has to come after that code.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
You ordered: Latte
Espresso is number 0
Latte is number 1
Mocha is number 2

macOS-style terminal window titled Terminal — EnumDeclare showing dotnet run printing You ordered: Latte, Espresso is number 0, Latte is number 1 and Mocha is number 2

Printing order wrote the member's name, Latte. Behind the names, the members got the numbers 0, 1 and 2 in the order they're declared, although the enum doesn't contain a single number.

The Complete File​

macOS-style code window titled Program.cs — EnumDeclare showing a Coffee variable set to Coffee.Latte, a line that prints it, three lines that print each member converted to int, and the enum Coffee declaration with the members Espresso, Latte and Mocha

Program.cs
Coffee order = Coffee.Latte;

Console.WriteLine($"You ordered: {order}");

// Each member stands for a number: 0, 1 and 2.
Console.WriteLine($"Espresso is number {(int)Coffee.Espresso}");
Console.WriteLine($"Latte is number {(int)Coffee.Latte}");
Console.WriteLine($"Mocha is number {(int)Coffee.Mocha}");

enum Coffee
{
Espresso,
Latte,
Mocha,
}

Common Mistakes​

MistakeWhat happensDo this instead
Declaring the enum above the code that runserror CS8803: Top-level statements must precede namespace and type declarations.Move the enum to the end of Program.cs, or into a file of its own
Leaving out the enum's name, as in Coffee order = Latte;error CS0103: The name 'Latte' does not exist in the current contextWrite the type's name first: Coffee.Latte. The C# specification: From all other code, the name of an enum member shall be qualified with the name of its enum type.
Ending each member with a semicolonerror CS1003: Syntax error, ',' expected, once for each memberSeparate the members with commas
Giving the variable a number, as in Coffee order = 1;error CS0266: Cannot implicitly convert type 'int' to 'Coffee'. An explicit conversion exists (are you missing a cast?)Give it a member, such as Coffee.Latte, so the code says which choice it means

FAQ​

Can the enum go in a file of its own?​

Yes. With the same enum Coffee in a Coffee.cs next to Program.cs, the line Coffee order = Coffee.Mocha; in Program.cs built and printed You ordered: Mocha.

What number type is behind an enum?​

int, unless you choose another. Enum.GetUnderlyingType(typeof(Coffee)) printed System.Int32, the .NET name for int. The reference: You can explicitly specify any other integral numeric type as an underlying type of an enumeration type.

What value does an enum variable have before you choose one?​

The member with the number 0. Coffee nothingChosen = default; printed Espresso, and a new Coffee[2] array held Espresso, Espresso. The reference: The default value of an enumeration type E is the value produced by expression (E)0, even if zero doesn't have the corresponding enum member.

Can an enum have methods?​

No. A method inside enum Coffee stopped the build with error CS1513: } expected and three more errors. The reference: You can't define a method inside the definition of an enumeration type. To add functionality to an enumeration type, create an extension member.

Conclusion​

Declare an enum with enum, a singular name and its members separated by commas, after the code that runs in Program.cs or in a file of its own. Write its values as Name.Member. Behind them are the numbers 0, 1, 2 and so on, in the order you wrote the members.

Additional Resources​