LFInteractive Docs
  • Programming Documentation
  • C#
    • Installing Visual Studio
    • Understanding C#
      • Namespaces
      • Classes
      • Enum
      • Variables
        • Static Types
        • Primitive Types
        • Access Modifiers
        • Variables vs Properties
        • Nullable Variables
      • Getters and Setters
      • Solution vs Project
      • Struct vs Class
      • Coding Conventions
      • Tasks and Async
        • Parallel Tasks
      • Methods
      • PreProcessor Statements
    • Creating Your First Console App
      • Class Library
        • Models
          • File Model
          • Result Model
        • Controllers
          • File Controller
          • File System Controller
      • Console App
        • Nuget Packages
        • Main Method
    • Minecraft.NET
      • Minecraft.NET Library
      • Modrinth.NET Library
      • CurseForge.NET Library
      • Fabric.NET Library
    • Common Lib
      • Strings
      • Advanced Network Client
      • Advanced Timer
      • Advanced File Info
      • Configuration File
      • Application Config
      • Database File
      • Crypt Class
  • C++
    • Networking
      • Windows Socket (Client)
    • cclip
    • VCPKG
    • spdlog
      • Getting Started
      • Patterns
Powered by GitBook
On this page
  • Declaring an Enum
  • Explicit Enum Values
  • Using Enums
  • Converting Enums

Was this helpful?

Edit on GitHub
  1. C#
  2. Understanding C#

Enum

Enums, short for "enumerations," are a fundamental data type in C#. They allow you to define a named set of constant values representing distinct integer values. Enums are often used to make code more readable and maintainable by providing meaningful names for numeric constants.

Declaring an Enum

You can declare an enum using the enum keyword in C#. Here's the basic syntax:

enum DaysOfWeek
{
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}

In this example, we've defined an enum called DaysOfWeek with seven named constants. By default, the underlying values are assigned starting from 0 and incrementing by 1 for each subsequent element. So, Sunday is assigned a value of 0, Monday is 1, and so on.

Explicit Enum Values

You can assign specific integer values to enum members if needed:

enum DaysOfWeek
{
    Sunday = 1,
    Monday = 2,
    Tuesday = 3,
    Wednesday = 4,
    Thursday = 5,
    Friday = 6,
    Saturday = 7
}

In this example, we've assigned custom values to each day of the week.

Using Enums

Once you've defined an enum, you can use it in your code. For example, you can declare variables of the enum type and assign enum values to them:

DaysOfWeek today = DaysOfWeek.Wednesday;

You can also use enums in switch statements to make your code more readable:

switch (today)
{
    case DaysOfWeek.Monday:
        Console.WriteLine("It's Monday.");
        break;
    case DaysOfWeek.Wednesday:
        Console.WriteLine("It's Wednesday.");
        break;
    // ...
    default:
        Console.WriteLine("It's not a weekday.");
        break;
}

Converting Enums

You can convert between enum values and their underlying integer values using casting:

int dayValue = (int)DaysOfWeek.Monday; // Converts Monday enum to 2
DaysOfWeek anotherDay = (DaysOfWeek)3; // Converts integer 3 to Tuesday

Enums are a useful feature in C# for improving code clarity, reducing the risk of errors, and making your code more maintainable by using meaningful names for constant values.

PreviousClassesNextVariables

Last updated 1 year ago

Was this helpful?