web training room for online courses
Learn C# programming Language, Free C# Tutorial for Beginners, C# Development Guide

Enums in C# Example

What is enum in C# .Net

In this tutorial we learn about enum in C#
Enum is a value type in .net data type, Enum is an abstract class that has some static methods to work with enum. Before we see some enum example, let's take a look at the methods of System.Enum.

Method Description
GetName Gets the name of the constant in the specified enumeration
GetNames Returns a string array of constant names
GetValues Returns an array of constant values

C# Enum Example

enum EventState
    Initiated = 1,
    Started = 2,
    Closed = 3,
    Canceled = 4

Enum GetNames Example
Here we see how to use enum (enumeration) in our c# code

foreach (string str in Enum.GetNames(typeof(EventState)))
Enum GetValues Example
foreach (int i in Enum.GetValues(typeof(EventState)))
//this will print the number 2

// this will print string "Started"

You may also check How to convert enum to generic list type

Name Email Website
Enums in C#
in C# Tutorials
Learn C# Online Tutorial