<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Using LINQ to foreach over an enum in C#</title>
	<atom:link href="http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c/feed" rel="self" type="application/rss+xml" />
	<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c</link>
	<description>A .NET developer in Vancouver</description>
	<pubDate>Thu, 24 Jul 2008 11:15:08 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: Florent</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8264</link>
		<dc:creator>Florent</dc:creator>
		<pubDate>Tue, 15 Apr 2008 06:58:35 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8264</guid>
		<description>Oups... T argument is missing :)
You should read as:
IEnumerable&#38;lgtT&#38;rgt myEnumerableOfEnum = (IEnuemrable&#38;lgtT&#38;rgt)Enum.GetValues(typeof(T));
and:
IList&#38;lgtT&#38;rgt myListOfEnum = (IList&#38;lgtT&#38;rgt)Enum.GetValues(typeof(T));</description>
		<content:encoded><![CDATA[<p>Oups... T argument is missing :)<br />
You should read as:<br />
IEnumerable&amp;lgtT&amp;rgt myEnumerableOfEnum = (IEnuemrable&amp;lgtT&amp;rgt)Enum.GetValues(typeof(T));<br />
and:<br />
IList&amp;lgtT&amp;rgt myListOfEnum = (IList&amp;lgtT&amp;rgt)Enum.GetValues(typeof(T));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Florent</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8263</link>
		<dc:creator>Florent</dc:creator>
		<pubDate>Tue, 15 Apr 2008 06:56:12 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8263</guid>
		<description>Hi guys :)
I think that "Cast" method is not mandatory... In fact, you can directly cast that Enum.GetValues return into IEnumerable. Because it's a simple array :)
So you can do like this:
IEnumerable myEnumerableOfEnum = (IEnuemrable)Enum.GetValues(typeof(T));
But you can do this too:
IList myListOfEnum = (IList)Enum.GetValues(typeof(T));
T[] myArrayOfEnum = (T[])Enum.GetValues(typeof(T));

I hope this will usefull :)</description>
		<content:encoded><![CDATA[<p>Hi guys :)<br />
I think that "Cast" method is not mandatory... In fact, you can directly cast that Enum.GetValues return into IEnumerable. Because it's a simple array :)<br />
So you can do like this:<br />
IEnumerable myEnumerableOfEnum = (IEnuemrable)Enum.GetValues(typeof(T));<br />
But you can do this too:<br />
IList myListOfEnum = (IList)Enum.GetValues(typeof(T));<br />
T[] myArrayOfEnum = (T[])Enum.GetValues(typeof(T));</p>
<p>I hope this will usefull :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8242</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Sun, 13 Apr 2008 07:37:21 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8242</guid>
		<description>&#62; hope you don't mind me reformatting it

No problem.

This is the second time I would have liked to write an extension method that extends the target's static methods (rather than instance).</description>
		<content:encoded><![CDATA[<p>&gt; hope you don't mind me reformatting it</p>
<p>No problem.</p>
<p>This is the second time I would have liked to write an extension method that extends the target's static methods (rather than instance).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damien Guard</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8239</link>
		<dc:creator>Damien Guard</dc:creator>
		<pubDate>Sat, 12 Apr 2008 09:30:01 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8239</guid>
		<description>That's nice! (hope you don't mind me reformatting it).  I did consider going with an extension method but my worry was having .Values appear for all classes.

[)amien</description>
		<content:encoded><![CDATA[<p>That's nice! (hope you don't mind me reformatting it).  I did consider going with an extension method but my worry was having .Values appear for all classes.</p>
<p>[)amien</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8238</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Sat, 12 Apr 2008 07:56:01 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8238</guid>
		<description>You can make use of an extension method to make this slightly nicer... but you call the extension against an instance of the enum (naming for the extension needs to be better):
&lt;pre class="code"&gt;
&lt;strong&gt;using&lt;/strong&gt; System;
&lt;strong&gt;using&lt;/strong&gt; System.Collections.Generic;
&lt;strong&gt;using&lt;/strong&gt; System.Linq;

&lt;strong&gt;static class&lt;/strong&gt; Enums {
	&lt;strong&gt;public static &lt;/strong&gt;IEnumerable&#60;T&#62; Values&#60;T&#62;(&lt;strong&gt;this&lt;/strong&gt; T en) &lt;strong&gt;where&lt;/strong&gt; T : &lt;strong&gt;struct&lt;/strong&gt; {
		&lt;strong&gt;if&lt;/strong&gt; (!&lt;strong&gt;typeof&lt;/strong&gt;(T).IsEnum)
			&lt;strong&gt;throw new&lt;/strong&gt; InvalidOperationException();
		&lt;strong&gt;foreach&lt;/strong&gt; (&lt;strong&gt;var&lt;/strong&gt; x &lt;strong&gt;in&lt;/strong&gt;Enum.GetValues(typeof(T)).Cast&#60;T&#62;())
			&lt;strong&gt;yield return&lt;/strong&gt; x;
	}
}

&lt;strong&gt;enum&lt;/strong&gt; Test {
	One,
	Two,
	Three
}

&lt;strong&gt;class&lt;/strong&gt; Program {
	&lt;strong&gt;static void&lt;/strong&gt; Main(&lt;strong&gt;string&lt;/strong&gt;[] args) {
		IEnumerable&#60;Test&#62; t = Test.One.Values();
		&lt;strong&gt;foreach&lt;/strong&gt; (&lt;strong&gt;var&lt;/strong&gt; x &lt;strong&gt;in&lt;/strong&gt; t)
			Console.WriteLine(x);
	}
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>You can make use of an extension method to make this slightly nicer... but you call the extension against an instance of the enum (naming for the extension needs to be better):</p>
<pre class="code">
<strong>using</strong> System;
<strong>using</strong> System.Collections.Generic;
<strong>using</strong> System.Linq;

<strong>static class</strong> Enums {
	<strong>public static </strong>IEnumerable&lt;T&gt; Values&lt;T&gt;(<strong>this</strong> T en) <strong>where</strong> T : <strong>struct</strong> {
		<strong>if</strong> (!<strong>typeof</strong>(T).IsEnum)
			<strong>throw new</strong> InvalidOperationException();
		<strong>foreach</strong> (<strong>var</strong> x <strong>in</strong>Enum.GetValues(typeof(T)).Cast&lt;T&gt;())
			<strong>yield return</strong> x;
	}
}

<strong>enum</strong> Test {
	One,
	Two,
	Three
}

<strong>class</strong> Program {
	<strong>static void</strong> Main(<strong>string</strong>[] args) {
		IEnumerable&lt;Test&gt; t = Test.One.Values();
		<strong>foreach</strong> (<strong>var</strong> x <strong>in</strong> t)
			Console.WriteLine(x);
	}
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8203</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Thu, 10 Apr 2008 21:25:13 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8203</guid>
		<description>Yuck. Equivalent Java code:

for (CustomerTypes customerType : CustomerTypes.values())

One of the rare occasions Java is more concise than C#, obviously ;)</description>
		<content:encoded><![CDATA[<p>Yuck. Equivalent Java code:</p>
<p>for (CustomerTypes customerType : CustomerTypes.values())</p>
<p>One of the rare occasions Java is more concise than C#, obviously ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damien Guard</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8199</link>
		<dc:creator>Damien Guard</dc:creator>
		<pubDate>Thu, 10 Apr 2008 19:56:37 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8199</guid>
		<description>Good question!

LINQ uses some specific compiler magic for the select/in/where/order keywords and also has a bunch of classes in the System.Linq namespace.  

To achieve the functionality requires these classes use Extension methods quite extensively.  It also takes advantage of Lambda expressions, anonymous types and is demonstrated often using type inference, all C# 3.0 features!

[)amien</description>
		<content:encoded><![CDATA[<p>Good question!</p>
<p>LINQ uses some specific compiler magic for the select/in/where/order keywords and also has a bunch of classes in the System.Linq namespace.  </p>
<p>To achieve the functionality requires these classes use Extension methods quite extensively.  It also takes advantage of Lambda expressions, anonymous types and is demonstrated often using type inference, all C# 3.0 features!</p>
<p>[)amien</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guy</title>
		<link>http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c#comment-8198</link>
		<dc:creator>Guy</dc:creator>
		<pubDate>Thu, 10 Apr 2008 19:26:18 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/?p=901#comment-8198</guid>
		<description>Great post and valuable information - bookmarked and will be used in the future. :)

Question: Is it actually the power of LINQ that you're using? i.e. Are extension methods part of LINQ or are they part of C# 3.0?</description>
		<content:encoded><![CDATA[<p>Great post and valuable information - bookmarked and will be used in the future. :)</p>
<p>Question: Is it actually the power of LINQ that you're using? i.e. Are extension methods part of LINQ or are they part of C# 3.0?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
