Iterate through Enumeration C#
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
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
Excellent solution buddy
Nazir
March 19, 2008 at 6:45 am
thx, its simple
Anonymous
April 25, 2008 at 8:38 am
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
foreach(SiteHelper.CurrenyList objCurrency in Enum.GetValues(typeof(SiteHelper.CurrenyList)))
{
Label1.Text = Label1.Text + objCurrency.ToString();
}
aa
September 14, 2008 at 11:27 am
// 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
// 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
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
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
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
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
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