C# Basics - Enums
Posted on June 26, 2024 (Last modified on October 11, 2024) • 1 min read • 169 wordsVideo is in Swedish
Enums in C# are used to define a set of named constants. They allow you to assign meaningful names to values, which can make your code more readable and maintainable.
Here is an example of how to use enums:
enum Color { Red, Green, Blue }
In this example, we have defined an enum called Color
with three possible values: Red
, Green
, and Blue
. We can then use these values in our code like this:
int redCount = 5;
int greenCount = 10;
int blueCount = 3;
if (redCount > greenCount && redCount > blueCount)
{
Console.WriteLine("Red is the most common color.");
}
In this example, we have three variables that represent the count of each color. We can then use an if
statement to determine which color is the most common by comparing their counts.
Enums are a powerful feature in C# and can be used in many different scenarios. They can help you write more readable code and make your code easier to maintain over time.
Swedish