471,317 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Checking an ENUM

Jon
Hello all,

Is there a way to loop through a ENUM, like using a foreach loop?

I'd also like to know if you can do a 'Contains' or like method on an ENUM.

Thanks all,

JY
Feb 10 '06 #1
3 1120
>Is there a way to loop through a ENUM, like using a foreach loop?

foreach (YourEnum e in Enum.GetValues(typeof(YourEnum)))

I'd also like to know if you can do a 'Contains' or like method on an ENUM.


Enum.IsDefined()
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 10 '06 #2
=?Utf-8?B?Sm9u?= <Jo*@discussions.microsoft.com> wrote in
news:88**********************************@microsof t.com:
Hello all,

Is there a way to loop through a ENUM, like using a foreach loop?


Take a look at Enum.GetValues(type), or Enum.GetNames(type).

-mdb
Feb 10 '06 #3
since an enumeration is (usually) integer based. you can also do
Enum.Parse(0), 1, 2, .... to get the values at the respective places
until an exception is thrown.

Feb 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Dave Robinson | last post: by
18 posts views Thread by Nebula | last post: by
2 posts views Thread by Pavils Jurjans | last post: by
31 posts views Thread by Michael C | last post: by
2 posts views Thread by Jarod_24 | last post: by
34 posts views Thread by Steven Nagy | last post: by
5 posts views Thread by Sin Jeong-hun | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.