Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

Learn Enums in C# Tutorial

In this article we learn about enum in C#

What is enum in C# .Net

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
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


Group Training