Skip to main content

How to Compare Enum Values in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

An enum gives a fixed set of values names, such as Sunny, Cloudy and Rainy, so code says Weather.Rainy instead of a mystery number like 2. In C#, compare enum values with == and != against those names.

Syntax​

Syntax
enum <EnumName>
{
<Member1>,
<Member2>,
}

if (<value> == <EnumName>.<Member>)
{
<statements>
}
PartWhat it does
enum <EnumName> { ... }Declares the enum and its named members.
<value> == <EnumName>.<Member>true when the value is that member. != is true when it isn't.
<EnumName>.<Member>Always written with the enum's name in front, such as Weather.Rainy.

Microsoft's enumeration types 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 Example​

A Weather enum has three members. Today's weather is Rainy: the example compares it with == and != and prints it.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which sets today to Weather.Rainy, prints Take an umbrella. when today equals Weather.Rainy, prints whether today isn't Weather.Sunny, prints today, and declares the Weather enum with Sunny, Cloudy and Rainy.

main.cs
Weather today = Weather.Rainy;

if (today == Weather.Rainy)
{
Console.WriteLine("Take an umbrella.");
}

Console.WriteLine(today != Weather.Sunny);
Console.WriteLine(today);

enum Weather
{
Sunny,
Cloudy,
Rainy,
}

In a file with top-level statements, the enum goes below the code that uses it.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
Take an umbrella.
True
Rainy

A zsh terminal where dotnet run main.cs prints Take an umbrella., True and Rainy.

today == Weather.Rainy was true, and today != Weather.Sunny printed True. Printing an enum value writes its name, Rainy, not its number. The command is the same in zsh, bash and PowerShell.

What Happens When You Compare with a Number​

Behind the names, the members are numbers: Sunny is 0, Cloudy 1 and Rainy 2. 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." But C# won't compare an enum with a number:

main.cs
Weather today = Weather.Rainy;
Console.WriteLine(today == 2);

enum Weather
{
Sunny,
Cloudy,
Rainy,
}
Output
/Users/you/demo/main.cs(2,19): error CS0019: Operator '==' cannot be applied to operands of type 'Weather' and 'int'

The build failed. Fix the build errors and run again.

That's deliberate: it keeps you from mixing up enums and plain numbers. When you really need the number, cast it. (int)today is 2, and (Weather)1 is Cloudy. Both printed exactly that in a quick check. The reference: "For any enumeration type, explicit conversions exist between the enumeration type and its underlying integral type."

Common Mistakes​

MistakeWhat happensDo this instead
Comparing with a number, as in today == 2error CS0019: Operator '==' cannot be applied to operands of type 'Weather' and 'int'Compare with the member: today == Weather.Rainy
Reordering or inserting members in an enum whose numbers are stored elsewhereStored numbers now mean different membersGive members explicit values, such as Rainy = 2, once numbers are saved
Comparing with 0 and expecting an errortoday == 0 compiles: the constant 0 converts to any enum typeCompare with a named member instead

Troubleshooting​

SymptomLikely causeFix
CS0019 with an enum and intComparing an enum with a numberCompare with a member, or cast with (int)
A number prints where you expected a nameThe value was cast to intPrint the enum value itself

FAQ​

Why does today == 0 compile when today == 2 doesn't?​

Zero is special. The reference: "C# allows implicit conversions from the literal value 0 to any enum type, and from const values equal to zero." A quick check with today == 0 built fine and printed False, because Rainy isn't 0.

Can I use a switch on an enum?​

Yes. A switch statement or expression with case Weather.Sunny: and the other members is a tidy alternative to a chain of if and else if comparisons.

Conclusion​

Compare enum values with == and != against named members, as in today == Weather.Rainy. Printing a value shows its name, a cast gives its number, and comparing with a plain number other than 0 stops the build with CS0019.

Additional Resources​