Codectionary / Developer documentation / C#

Enums

An enum defines a fixed set of named integer constants, like the days of the week or the possible states of an order. Enums are type-safe - the compiler ensures only valid enum values can be assigned - and each constant maps to an underlying integer by default (starting at 0), though you can assign custom values explicitly.

Syntax

enum EnumName {\n  Constant1, Constant2\n}

Examples

Basic Enum

Defining and using a simple set of named constants.

public enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }

Day today = Day.Wednesday;

if (today == Day.Saturday || today == Day.Sunday)
{
    Console.WriteLine("Weekend!");
}
else
{
    Console.WriteLine("Weekday");
}

Enum with switch

Enums integrate naturally with switch statements and expressions.

public enum TrafficLight { Red, Yellow, Green }

string GetAction(TrafficLight light) => light switch
{
    TrafficLight.Red => "Stop",
    TrafficLight.Yellow => "Slow down",
    TrafficLight.Green => "Go",
    _ => "Unknown"
};

Console.WriteLine(GetAction(TrafficLight.Green));  // Go

Custom Underlying Values

Enum constants map to integers by default (starting at 0), but you can assign your own values explicitly.

public enum HttpStatus
{
    Ok = 200,
    NotFound = 404,
    ServerError = 500
}

HttpStatus status = HttpStatus.NotFound;
Console.WriteLine((int)status);   // 404
Console.WriteLine(status);         // NotFound

Parsing and Iterating Enums

Converting strings to enum values, and looping over every defined constant.

public enum Size { Small, Medium, Large }

Size chosen = Enum.Parse<Size>("Medium");
Console.WriteLine(chosen);  // Medium

foreach (Size s in Enum.GetValues<Size>())
{
    Console.WriteLine($"{s} = {(int)s}");
}

Best practices

  • Use enums instead of plain int or string constants whenever a variable should only take one of a fixed, known set of values - it gives compile-time safety
  • Take advantage of switch expressions with enums for exhaustive, readable branching on the possible values
  • Assign explicit underlying values (like HTTP status codes) when the numeric value has external meaning that must stay stable
  • Use PascalCase for enum names and their members, following standard C# naming conventions (not ALL_CAPS as in some other languages)

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Classes & Objects
A class is a blueprint for creating objects, bundling related data (fields/properties) and behavior (methods) together. C# is a fully object-oriented language - every application has at least one class, typically with a Main method as its entry point. An object is a specific instance of a class, created with the new keyword, with its own independent copy of the class's instance data.
Constructors
A constructor is a special method that runs automatically when an object is created with new, used to initialize the object's state. A constructor shares its name with the class and has no return type. C# supports constructor overloading (multiple constructors with different parameter lists) and constructor chaining with this(...), where one constructor calls another in the same class to avoid duplicating initialization logic.
Properties (get/set)
Properties are C#'s idiomatic way to expose class data through accessor-like syntax while still allowing controlled access via get and set. Unlike a plain public field, a property can validate a value before it's set, compute a value on the fly, or restrict access to read-only. Auto-implemented properties (using { get; set; } with no body) provide a concise shorthand when no custom logic is needed.
Access Modifiers
Access modifiers control the visibility of classes, fields, methods, and properties from other parts of a program. C# provides public (accessible from anywhere), private (accessible only within the declaring class - the default for class members), protected (accessible within the class and its subclasses), internal (accessible only within the same assembly/project), and combinations like protected internal and private protected.