Manish Pansiniya's Blog

.NET, C#, Javascript, ASP.NET and lots more…:)

Iterate through Enumeration C#

with 17 comments

First create the array using following code:

Array lstProvider = System.Enum.GetValues( typeof(CommonShared.EnumProvider)); Then you can directly call the following foreach loop:

foreach (CommonShared.EnumProvider enProvider in lstProvider)
{

}

Simple huh :P

Written by Manish

September 27, 2007 at 12:56 pm

Posted in .NET

17 Responses

Subscribe to comments with RSS.

  1. one line:
    foreach(MyEnum in Enum.GetValues(typeof(MyEnum)))

    Anonymous

    January 8, 2008 at 4:52 pm

    • Brilliant one line! (anonymous from 8.01.2008)

      b

      February 14, 2010 at 7:43 pm

    • all in one!
      this.comboBox1.DataSource=Enum.GetValues(typeof(MyEnum))

      b

      February 14, 2010 at 7:58 pm

  2. Excellent solution buddy

    Nazir

    March 19, 2008 at 6:45 am

  3. thx, its simple

    Anonymous

    April 25, 2008 at 8:38 am

  4. example

    foreach(SiteHelper.CurrenyList objCurrency in Enum.GetValues(typeof(SiteHelper.CurrenyList)))
    {
    Label1.Text = Label1.Text + bba.ToString();
    }

    aa

    September 14, 2008 at 11:26 am

  5. foreach(SiteHelper.CurrenyList objCurrency in Enum.GetValues(typeof(SiteHelper.CurrenyList)))
    {
    Label1.Text = Label1.Text + objCurrency.ToString();
    }

    aa

    September 14, 2008 at 11:27 am

  6. // Here is how you retrieve the integer value of the enumeration

    foreach( MyEnum objEnum
    in Enum.GetValues(typeof(AppealsWorkflow)) )
    {
    // Retrieve integer value of enumeration
    intTemp = System.Convert.ToInt32(
    Enum.Parse(typeof(MyEnum),objEnum.ToString()));

    Skip Cherniss

    October 7, 2008 at 9:50 pm

  7. // Just to add something, Here is how you Iterate with Enum Argument.

    public void ParseRequiredFields(Enum oBankEnum)
    {
    System.Type oBankEnumType = oBankEnum.GetType();
    foreach (String objBankEnum in System.Enum.GetNames(oBankEnumType))
    {
    objBankEnum.ToString();
    }

    }

    Jet

    February 10, 2009 at 8:27 am

    • how to call Function ParseRequiredFields()

      suppose i have an enum of name “Test”.

      e.g

      Test
      {
      Add=0,
      Delete=1,
      Modify=2
      }

      now how to send Test enum into ParseRequiredFields() as parameter .
      I need the c# syntax.
      Please help me out ASAP.

      Debasish Das

      November 23, 2010 at 11:18 am

    • I dont know exactly where you required this. But following is the sytanx for calling it. And it will take type of enum in function and iterate.

      ParseRequiredFields(Test.Add);

      Manish

      November 23, 2010 at 12:29 pm

  8. Jet, thank you very much for the followup coding. That was exactly what I was looking for.

    PaulN

    February 17, 2009 at 8:49 pm

  9. Jet,
    Thanks a lot.. It really works..

    Mandakini

    December 10, 2009 at 12:06 pm

    • how to call Function ParseRequiredFields()

      suppose i have an enum of name “Test”.

      e.g

      Test
      {
      Add=0,
      Delete=1,
      Modify=2
      }

      now how to send Test enum into ParseRequiredFields() as parameter .
      I need the c# syntax.
      Please help me out ASAP.

      Debasish Das

      November 23, 2010 at 11:33 am

  10. Excellent – I needed to iterate through enums to get at a [Description] attribute I placed over each enum value.

    Bob Cummins

    May 20, 2010 at 6:17 am

  11. enum food{Apple, ApplePie, AppleJuice,Pizza};
    void print()
    {
    foreach (food value in Enum.GetValues(typeof(food)))
    {
    Console.WriteLine(value);
    }
    }

    Isaac Darlong

    August 8, 2010 at 1:39 am

  12. This is one of the best answer so far, I have read online. Just useful information. Very well presented. Thanks for sharing with us. I had found another nice post with wonderful explanation on Enumeration in c#, which is also helped me to complete my task. For more details of that post check out this link….

    http://mindstick.com/Articles/ade257fc-7058-4f60-a0fe-85c7ca52f004/?Enumeration%20in%20c#

    Thanks everyone for your precious post.

    Pravesh Singh

    January 9, 2012 at 4:31 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: