C# Enum Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
The [Flags]
attribute in C# is used to indicate that an enumeration type is a bit field, meaning that each value represents a binary bit that can be combined with other values to create a set of possible combinations. When the [Flags]
attribute is applied to an enumeration, it enables bitwise operations, such as OR and AND, to be used with its values.
To compare two Enum values, you can use the Equals
method, which checks if two objects are equal. Alternatively, you can use the ==
operator, which compares the underlying integer values of the Enum values.
To check if a given value is included in an Enum, you can use the Enum.IsDefined
method, which returns a boolean value indicating whether a specified value exists in the specified enumeration type.
To convert an Enum to another type of Enum, you can cast the Enum value to the desired type using the as
operator, and then use the Enum.Parse
method to convert it to the new Enum type. Here's an example:
enum MyEnum1 { Value1 = 1, Value2 = 2, Value3 = 3 } enum MyEnum2 { ValueA = 1, ValueB = 2, ValueC = 3 } MyEnum1 myEnum1 = MyEnum1.Value2; MyEnum2 myEnum2 = (MyEnum2)myEnum1; // cast to desired type
Alternatively, you can use the Enum.ToObject
method to convert the Enum value to an object
, and then cast it to the desired type. Here's an example:
MyEnum1 myEnum1 = MyEnum1.Value2; MyEnum2 myEnum2 = (MyEnum2)Enum.ToObject(typeof(MyEnum2), myEnum1); // convert to object and cast to desired type
How to declare enums in C#: Declaring an enum in C# is straightforward. Enums provide a way to define a set of named integral constants.
enum DaysOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
Enum values and types in C#: Enum values are integral constants, and the underlying type of an enum can be specified.
enum DaysOfWeek : byte { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
Using enums as flags in C#: Enums can be used as flags by assigning each value a unique power of 2.
[Flags] enum Permissions { Read = 1, Write = 2, Execute = 4 } Permissions userPermission = Permissions.Read | Permissions.Write;
Iterating over enum values in C#:
Enum values can be iterated using Enum.GetValues
.
foreach (DaysOfWeek day in Enum.GetValues(typeof(DaysOfWeek))) { Console.WriteLine(day); }
C# enum vs. constants: Enums are a more structured way to represent a set of named constants compared to traditional constants.
enum Season { Spring, Summer, Autumn, Winter } // vs. const int Spring = 0; const int Summer = 1; const int Autumn = 2; const int Winter = 3;
Enums with associated values in C#:
Enums can have associated values using the enum
's underlying type.
enum Status : byte { Inactive = 0, Active = 1 } Status userStatus = Status.Active;
Enum.TryParse method in C#:
Using Enum.TryParse
for safe parsing of enum values.
if (Enum.TryParse("Monday", out DaysOfWeek result)) { // Successfully parsed }
Casting enums in C#: Casting enums to their underlying type or vice versa.
DaysOfWeek day = DaysOfWeek.Monday; int dayValue = (int)day; // or int dayValue = 1; DaysOfWeek day = (DaysOfWeek)dayValue;
Switch statements with enums in C#: Using switch statements with enums for improved readability.
DaysOfWeek day = DaysOfWeek.Monday; switch (day) { case DaysOfWeek.Monday: Console.WriteLine("It's Monday!"); break; // other cases... }
Adding attributes to enums in C#: Adding attributes to enum values for additional metadata.
public enum Colors { [Description("Red color")] Red, [Description("Green color")] Green, [Description("Blue color")] Blue }
Using enums in switch expressions in C#: Switch expressions provide a concise way to handle enums.
DaysOfWeek day = DaysOfWeek.Monday; string message = day switch { DaysOfWeek.Monday => "It's Monday!", DaysOfWeek.Tuesday => "It's Tuesday!", // other cases... _ => "Not a recognized day" };
Nullable enums in C#: Enums can be made nullable using the nullable reference types feature.
DaysOfWeek? nullableDay = null;
Working with enums in LINQ queries in C#: Using enums in LINQ queries for filtering and sorting.
var weekends = Enum.GetValues(typeof(DaysOfWeek)) .Cast<DaysOfWeek>() .Where(day => day == DaysOfWeek.Saturday || day == DaysOfWeek.Sunday);