<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ticklish Techs &#187; LINQ</title>
	<atom:link href="http://www.ticklishtechs.net/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ticklishtechs.net</link>
	<description>a mostly .NET but also some other cool techs blog</description>
	<lastBuildDate>Fri, 23 Jul 2010 06:07:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The List&lt;T&gt;.ForEach() method</title>
		<link>http://www.ticklishtechs.net/2008/07/23/the-listtforeach-method/</link>
		<comments>http://www.ticklishtechs.net/2008/07/23/the-listtforeach-method/#comments</comments>
		<pubDate>Wed, 23 Jul 2008 16:43:51 +0000</pubDate>
		<dc:creator>Benjamin Schröter</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[dotnet3.5]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.ticklishtechs.net/2008/07/23/the-listtforeach-method/</guid>
		<description><![CDATA[We all know and love the foreach keyword in C# and use it for loops every day:
List&#60;int&#62; list = new List&#60;int&#62;();
/* fill the list */
foreach (int i in list)
    Console.WriteLine(i);

But the generic List&#60;T&#62; class contains a useful method for small loops, too. The method is called ForEach() and has only one parameter: [...]]]></description>
			<content:encoded><![CDATA[<p>We all know and love the foreach keyword in C# and use it for loops every day:</p>
<pre class="code"><span style="color: #2b91af;">List</span>&lt;<span style="color: blue;">int</span>&gt; list = <span style="color: blue;">new </span><span style="color: #2b91af;">List</span>&lt;<span style="color: blue;">int</span>&gt;();
<span style="color: green;">/* fill the list */
</span><span style="color: blue;">foreach </span>(<span style="color: blue;">int </span>i <span style="color: blue;">in </span>list)
    <span style="color: #2b91af;">Console</span>.WriteLine(i);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>But the generic <code>List&lt;T&gt; </code>class contains a useful method for small loops, too. The method is called <code>ForEach()</code> and has only one parameter: an <code>Action&lt;T&gt;</code>.</p>
<h3>delegates: Action&lt;&gt;, Func&lt;&gt; and Predicate&lt;&gt;</h3>
<p>The Framework 3.5 defines a few generic delegate types for actions and functions. In this case a function is something that returns a value (think back to your math class) and actions are something that do not return a value but do something (maybe think back to physics class). All these delegates are defined with zero, one, two, three and four parameters:</p>
<p><span style="color: blue;">public delegate void</span><span style="color: #2b91af;">Action</span>();<br />
<span style="color: blue;">public delegate void</span><span style="color: #2b91af;">Action</span>&lt;T&gt;(T obj);<br />
<span style="color: blue;">public delegate void</span><span style="color: #2b91af;">Action</span>&lt;T1, T2&gt;(T1 arg1, T2 arg2);<br />
<span style="color: blue;">public delegate void</span><span style="color: #2b91af;">Action</span>&lt;T1, T2, T3&gt;(T1 arg1, T2 arg2, T3 arg3);<br />
<span style="color: blue;">public delegate void</span><span style="color: #2b91af;">Action</span>&lt;T1, T2, T3, T4&gt;(T1 arg1, T2 arg2, T3 arg3, T4 arg4);</p>
<p><span style="color: blue;">public delegate</span>TResult <span style="color: #2b91af;">Func</span>&lt;TResult&gt;();<br />
<span style="color: blue;">public delegate</span>TResult <span style="color: #2b91af;">Func</span>&lt;T, TResult&gt;(T arg);<br />
<span style="color: blue;">public delegate</span>TResult <span style="color: #2b91af;">Func</span>&lt;T1, T2, TResult&gt;(T1 arg1, T2 arg2);<br />
<span style="color: blue;">public delegate</span>TResult <span style="color: #2b91af;">Func</span>&lt;T1, T2, T3, TResult&gt;(T1 arg1, T2 arg2, T3 arg3);<br />
<span style="color: blue;">public delegate</span>TResult <span style="color: #2b91af;">Func</span>&lt;T1, T2, T3, T4, TResult&gt;(T1 arg1, T2 arg2, T3 arg3, T4 arg4);</p>
<p>And there is one more delegate. The <code>Predicate&lt;T&gt;</code> (philosophy class, you know). A predicate comes to a logical decision (true or false; bool return value) based on a single object:<br />
<span style="color: blue;">public delegate bool </span><span style="color: #2b91af;">Predicate</span>&lt;T&gt;(T obj);</p>
<p>All these delegates are generic so you can use them in a type save way with the type parameters you need.</p>
<h3>Back to the ForEach() method</h3>
<p>As I explained above, the <code>ForEach()</code> method takes an <code>Action&lt;T&gt;</code> parameter. In my example this is an <code>Action&lt;int&gt; </code>delegate:</p>
<pre class="code">list.ForEach( <span style="color: blue;">delegate</span>(<span style="color: blue;">int </span>i) {<span style="color: #2b91af;">Console</span>.WriteLine(i);} );</pre>
<p>This anonymous delegate declaration is not that nice, but we can use Lamda types here:</p>
<pre class="code">list.ForEach(i =&gt; <span style="color: #2b91af;">Console</span>.WriteLine(i));</pre>
<p><a href="http://11011.net/software/vspaste"></a>But let&#8217;s take a look at the method we&#8217;re calling: <code>void Console.WriteLine(int)</code>. That is exactly the definition of the needed Action here. So we can write the code line even shorter:</p>
<pre class="code">list.ForEach( <span style="color: #2b91af;">Console</span>.WriteLine );</pre>
<p><a href="http://11011.net/software/vspaste"></a>I like this code. It is very short, elegant and readable. It says: &#8220;For each [entry of] the list: write it out&#8221;. Great.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ticklishtechs.net/2008/07/23/the-listtforeach-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
