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

Understanding C# Enumerations (Enums) – A Comprehensive Guide

An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.

C# enumerations are value data type. In other words, enumeration contains its own values and cannot inherit or cannot pass inheritance.

Declaring enum Variable

The general syntax for declaring an enumeration is −

enum <enum_name> {
   enumeration list 
};

Where,

Each of the symbols in the enumeration list stands for an integer value, one greater than the symbol that precedes it. By default, the value of the first enumeration symbol is 0. For example −

enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

Example

The following example demonstrates use of enum variable −

Live Demo
using System;

namespace EnumApplication {
   class EnumProgram {
      enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

      static void Main(string[] args) {
         int WeekdayStart = (int)Days.Mon;
         int WeekdayEnd = (int)Days.Fri;
         
         Console.WriteLine("Monday: {0}", WeekdayStart);
         Console.WriteLine("Friday: {0}", WeekdayEnd);
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following result −

Monday: 1
Friday: 5

C Language

  1. C# Hello World – Building Your First C# Application
  2. Understanding C# Keywords and Identifiers: Rules, Lists, and Best Practices
  3. Master C# Variables & Primitive Data Types: A Complete Guide
  4. Mastering C Enums: Definition, Declaration, and Flag Operations
  5. Master Java Enums: A Complete Guide to Enums & Enum Classes
  6. Java Enum Constructors Explained with Practical Example
  7. Mastering String Representations in Java Enums
  8. Mastering Java EnumMap: Efficient Key-Value Mapping with Enums
  9. C# Enumerations (Enums) – Definition, Example, and Usage
  10. Understanding C# Enumerations (Enums) – A Comprehensive Guide