<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C# - Iterating through an Enum</title><link>http://weblogs.asp.net/alnurismail/archive/2008/10/06/c-iterating-through-an-enum.aspx</link><description>Today I came across a situation where iterating through an Enum would save me about 10 lines of code and some time. The quickest way is as follows: foreach (MyEnum value in Enum .GetValues( typeof (MyEnum))) { //... }</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: C# - Iterating through an Enum</title><link>http://weblogs.asp.net/alnurismail/archive/2008/10/06/c-iterating-through-an-enum.aspx#6662520</link><pubDate>Tue, 07 Oct 2008 13:49:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6662520</guid><dc:creator>alnurismail</dc:creator><author>alnurismail</author><description>&lt;p&gt;Krzysztof - good catch. I should have been more specific and noted that this approach has only been tested with mutually exclusive elements.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Alnur&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6662520" width="1" height="1"&gt;</description></item><item><title>re: C# - Iterating through an Enum</title><link>http://weblogs.asp.net/alnurismail/archive/2008/10/06/c-iterating-through-an-enum.aspx#6662175</link><pubDate>Tue, 07 Oct 2008 11:34:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6662175</guid><dc:creator>Drakiula</dc:creator><author>Drakiula</author><description>&lt;p&gt;I am doing this to avoid any casts:&lt;/p&gt;
&lt;p&gt;public static IEnumerable&amp;lt;TEnum&amp;gt; EnumAsEnumerable&amp;lt;TEnum&amp;gt;()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var enumType = typeof(TEnum);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if (enumType == typeof(Enum))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw new ArgumentException(&amp;quot;typeof(TEnum) == System.Enum&amp;quot;, &amp;quot;TEnum&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if (!(enumType.IsEnum))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw new ArgumentException(String.Format(&amp;quot;typeof({0}).IsEnum == false&amp;quot;, enumType), &amp;quot;TEnum&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return Enum.GetValues(enumType).OfType&amp;lt;TEnum&amp;gt;();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6662175" width="1" height="1"&gt;</description></item><item><title>re: C# - Iterating through an Enum</title><link>http://weblogs.asp.net/alnurismail/archive/2008/10/06/c-iterating-through-an-enum.aspx#6661800</link><pubDate>Tue, 07 Oct 2008 09:03:36 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6661800</guid><dc:creator>Muhammad Shahid</dc:creator><author>Muhammad Shahid</author><description>&lt;p&gt;nice and quikest way to iterate enumeration&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6661800" width="1" height="1"&gt;</description></item><item><title>re: C# - Iterating through an Enum</title><link>http://weblogs.asp.net/alnurismail/archive/2008/10/06/c-iterating-through-an-enum.aspx#6661603</link><pubDate>Tue, 07 Oct 2008 08:03:44 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6661603</guid><dc:creator>Krzysztof Koźmic</dc:creator><author>Krzysztof Koźmic</author><description>&lt;p&gt;Notice what happens when your enum has [FlagsAttribute] on it. This may not be the behavior you want.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6661603" width="1" height="1"&gt;</description></item><item><title>re: C# - Iterating through an Enum</title><link>http://weblogs.asp.net/alnurismail/archive/2008/10/06/c-iterating-through-an-enum.aspx#6660715</link><pubDate>Tue, 07 Oct 2008 02:26:20 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6660715</guid><dc:creator>anytao</dc:creator><author>anytao</author><description>&lt;p&gt;Smart idea;-)&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6660715" width="1" height="1"&gt;</description></item></channel></rss>