C# Enums


An enum (short for "enumeration") in C# is a distinct value type that consists of a set of named constants called enumerators. Enums are used to represent a collection of related constants in a type-safe way, which improves code readability and maintainability. They allow you to define a variable that can hold a set of predefined values, making it easier to work with a fixed set of related options.

Key Features of Enums

  1. Type Safety: Enums provide type safety by allowing only predefined constants to be assigned to the variable.

  2. Underlying Type: By default, the underlying type of an enum is int, but you can specify another integral type (byte, sbyte, short, ushort, int, uint, long, or ulong).

  3. Implicit Conversion: Enums can be implicitly converted to their underlying type (e.g., int), but explicit conversion is required when converting back to the enum type.

  4. Custom Values: You can assign custom integer values to the enumerators.

  5. Flags: Enums can be combined using bitwise operations if they are marked with the [Flags] attribute, allowing for more flexible representations of combinations of values.

Defining an Enum

Here's how to define an enum in C#:

csharp
1public enum DaysOfWeek 2{ 3 Sunday, // 0 4 Monday, // 1 5 Tuesday, // 2 6 Wednesday, // 3 7 Thursday, // 4 8 Friday, // 5 9 Saturday // 6 10}

In this example, DaysOfWeek is an enum with seven named constants representing the days of the week. By default, the first enumerator (Sunday) has a value of 0, and each subsequent enumerator increases by 1.

Using Enums

You can use enums in various ways, such as declaring variables, comparing values, and iterating through the values.

Example 1: Declaring and Using an Enum

csharp
1using System; 2 3public class Program 4{ 5 public enum DaysOfWeek 6 { 7 Sunday, 8 Monday, 9 Tuesday, 10 Wednesday, 11 Thursday, 12 Friday, 13 Saturday 14 } 15 16 public static void Main() 17 { 18 DaysOfWeek today = DaysOfWeek.Wednesday; 19 20 if (today == DaysOfWeek.Wednesday) 21 { 22 Console.WriteLine("It's Wednesday!"); 23 } 24 } 25}

Example 2: Custom Values

You can assign specific values to the enumerators:

csharp
1public enum HttpStatusCode 2{ 3 OK = 200, 4 NotFound = 404, 5 InternalServerError = 500 6}

Example 3: Specifying Underlying Type

You can specify a different underlying type for the enum:

csharp
1public enum ErrorCode : byte 2{ 3 None = 0, 4 NotFound = 1, 5 AccessDenied = 2, 6 ServerError = 3 7}

Example 4: Using the [Flags] Attribute

Enums can represent bit fields when you apply the [Flags] attribute:

csharp
1[Flags] 2public enum FileAccess 3{ 4 Read = 1, // 0001 5 Write = 2, // 0010 6 Execute = 4, // 0100 7 ReadWrite = Read | Write // 0011 8} 9 10public class Program 11{ 12 public static void Main() 13 { 14 FileAccess access = FileAccess.Read | FileAccess.Write; 15 16 Console.WriteLine(access); // Output: Read, Write 17 Console.WriteLine(access.HasFlag(FileAccess.Read)); // Output: True 18 Console.WriteLine(access.HasFlag(FileAccess.Execute)); // Output: False 19 } 20}

Talk to us?

Post your blog