Home » Tutorials » C# » Enumerate Enum C# with Examples

Enumerate Enum C# with Examples

Difference between Hashtable and Dictionary
Find Most Expensive Stored Procedure Sql Server

How to enumerate Enum C#

Problem: We have enum declared in our class and we want to enumerate enum. How could we enumerate enum let’s together try to understand. Sometimes we get error message like “Enum is a type but used as a variable”. So how can we overcome from this issue!!!

Enum-prob-Enumeration

Enum is a type but used as a variable

 

Solution:

Let’s say we have a below enum in our code and we want to enumerate enum of DayOfWeek

There are two ways to enumerate enum:

  1. var values = Enum.GetValues(typeof(myenum))
  2. var values = Enum.GetNames(typeof(myenum))

The first will give you values in form on a array of object, and the second will give you values in form of array of String.

Find below code snippet for solution of “How to enumerate Enum in C#?”

We could also use alternate way of implementation as below:

Output:

Enumerate-Enum-Output

 

Folks, this is how we could enumerate Enum in C#. As I have explained two different ways to enumerate Enum, if anyone is having another way to enumerate Enum in C#, kindly share with us.

About

Hello friends, My self Dhruv Sheth, I have more than 6 years of Industrial experience as a Software Developer. In my career I got chance to work with asp.net, c#,Web API, JavaScript, JQuery, WPF, WCF , Windows Application, PostScript, vb.net, GhostScript etc. So I have decided to share my knowledge with everyone and finally I have come up with TechnoThirsty.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Time limit is exhausted. Please reload CAPTCHA.




DISCLAIMER:
The content is copyrighted to technothirsty.com and may not be reproduced on other websites without permission from the owner.You may contact us using the information below.