Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C# Enumerations (Enums) – Definition, Example, and Usage

C# Enumerations

Enumerations, or enums, let you define a fixed set of named constants in C#. They’re ideal for representing discrete values such as days of the week, months, or status codes.

Below we walk through a simple, real‑world example that shows how to declare an enum, assign values, and use it in your code.

C# Enumerations (Enums) – Definition, Example, and Usage

using System;

namespace DemoApplication
{
    class Program
    {
        enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat }

        static void Main(string[] args)
        {
            Console.WriteLine(Days.Sun);
            Console.ReadKey();
        }
    }
}

Code Breakdown

  1. Enum declaration: enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat } creates a new type named Days with seven named values.
  2. Usage: Console.WriteLine(Days.Sun); prints the enum value to the console.

If you run this program, you’ll see the following output:

Output

C# Enumerations (Enums) – Definition, Example, and Usage

The console displays Sun, confirming that the enum value is correctly referenced.

C Language

  1. C++ For Loops Explained: Syntax, Workflow, and Practical Examples
  2. Mastering std::stack in C++: A Comprehensive Guide with Practical Examples
  3. C++ Structs Explained with a Practical Example
  4. C++ Classes & Objects: A Practical Guide with Code Examples
  5. C++ Polymorphism Explained: Practical Examples & Key Concepts
  6. Mastering std::list in C++: Syntax, Functions & Practical Examples
  7. Mastering C# Variables & Operators: Practical Examples & Explanations
  8. Understanding C# Interfaces: Definition, Example, and Practical Use
  9. C# Windows Forms Development: A Hands‑On Tutorial
  10. Understanding C# Enumerations (Enums) – A Comprehensive Guide