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)))
{
    Console.WriteLine(str);
}
Enum GetValues Example
foreach (int i in Enum.GetValues(typeof(EventState)))
{
    Console.WriteLine(i);
}
//this will print the number 2
Console.WriteLine((int)EventState.Started);            

// this will print string "Started"
Console.WriteLine(EventState.Started);
}

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

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


Consulting