Skip to content

Latest commit

 

History

History
51 lines (34 loc) · 2.12 KB

enum.md

File metadata and controls

51 lines (34 loc) · 2.12 KB

Enum

The Concept

Sometimes also referred to as an Enumeration or Enumeration type, an enum represents a fixed set of named constants. Its chief purpose is to provide a type-safe way of interacting with numeric constants, limiting the available values to a pre-defined set.

In some languages, an enum is nothing more than syntactic sugar over using integers. Other languages implement enums as actual types, with the ability to add behavior to an enum.

What to cover

  • Language-specific implementation: is it syntactic sugar over an integer type, or is there more to it?
  • Defining an enum: how to define an enum.
  • Using an enum value: how to use an enum value, e.g. by assigning it or comparing it to another enum value.
  • Converting from and to string values: how to convert a string to an enum value and vice versa.

Exercises

Log Levels

This exercise extracts log level information from log lines. The reference implementation (C#) teaches:

  • Defining an enum.
  • Assigning values to an enum's members.
  • Comparing enum values.
  • Converting a string to an enum.
  • Converting an enum to a string.

Implementations

Track Exercise Changes
C# enums None

Permissions

This exercise works with user account permissions. The reference implementation (C#) teaches:

  • Defining as enum as a flags enum, that is a single enum instance can represent multiple values being set.
  • Setting and unsetting flags using bitwise operators.
  • Checking for a flag being set on an enum instance.

Implementations

Track Exercise Changes
C# enums None