<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>ProgramGood.Net - Events and Delegates</title>
    <link>http://www.programgood.net/</link>
    <description>The journey to becoming a great programmer</description>
    <language>en-us</language>
    <copyright>Dave Mateer</copyright>
    <lastBuildDate>Mon, 27 Jul 2009 05:25:04 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>davemateer@gmail.com</managingEditor>
    <webMaster>davemateer@gmail.com</webMaster>
    <item>
      <trackback:ping>http://www.programgood.net/Trackback.aspx?guid=2b9daa70-1e45-4039-ac58-a4ee3d4c6225</trackback:ping>
      <pingback:server>http://www.programgood.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.programgood.net/PermaLink,guid,2b9daa70-1e45-4039-ac58-a4ee3d4c6225.aspx</pingback:target>
      <dc:creator>Dave Mateer</dc:creator>
      <wfw:comment>http://www.programgood.net/CommentView,guid,2b9daa70-1e45-4039-ac58-a4ee3d4c6225.aspx</wfw:comment>
      <wfw:commentRss>http://www.programgood.net/SyndicationService.asmx/GetEntryCommentsRss?guid=2b9daa70-1e45-4039-ac58-a4ee3d4c6225</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Thanks to:<br />
http://davidhayden.com/blog/dave/archive/2006/05/28/2974.aspx<br /><br />
Anon methods, delegates, predicates.  Anon methods are simple shortcuts so don't
have to declare a predicate method.<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"></span><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Collections.Generic; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Linq; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Text; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> TestAnonymousMethods
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> Program
{ <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
using an anon method</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> Mainx(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span>[]
args) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>[]
_integers <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>[]
evenIntegers <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> Array.FindAll(_integers, <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
this is the anonymous method below</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">delegate</span>(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span> integer)
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> (integer <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">%</span> 2
== 0); } ); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">foreach</span> (<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span> integer <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">in</span> _integers)
Console.WriteLine(integer); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">foreach</span> (<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span> integer <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">in</span> evenIntegers)
Console.WriteLine(integer); } <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
not using anon method</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> Mainy(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span>[]
args) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>[]
_integers <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
passing in IsEven..a delegate which represents the IsEven method</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>[]
evenIntegers <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> Array.FindAll(_integers,
IsEven); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">foreach</span> (<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span> integer <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">in</span> _integers)
Console.WriteLine(integer); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">foreach</span> (<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span> integer <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">in</span> evenIntegers)
Console.WriteLine(integer); } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">bool</span> IsEven(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span> integer)
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> (integer <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">%</span> 2
== 0); } <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
not using anon method and looking at predicate / delegate of Array.FindAll</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> Main(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span>[]
args) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>[]
_integers <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
a predicate is expected</span><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
passing in IsEven..a delegate which represents the IsEven method</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>[]
whatIsInBoth <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> Array.FindAll(_integers,
IsDaveSequence); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">foreach</span> (<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span> integer <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">in</span> whatIsInBoth)
Console.WriteLine(integer); } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">bool</span> IsDaveSequence(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span> integer)
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">bool</span> returnState <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">false</span>; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">if</span> ((integer
== 1) || (integer == 3) || (integer ==4)) returnState <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">true</span>; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> returnState;
} } } </span></pre></span></pre><p></p><img width="0" height="0" src="http://www.programgood.net/aggbug.ashx?id=2b9daa70-1e45-4039-ac58-a4ee3d4c6225" /></body>
      <title>Anonymous Methods</title>
      <guid isPermaLink="false">http://www.programgood.net/PermaLink,guid,2b9daa70-1e45-4039-ac58-a4ee3d4c6225.aspx</guid>
      <link>http://www.programgood.net/2009/07/27/AnonymousMethods.aspx</link>
      <pubDate>Mon, 27 Jul 2009 05:25:04 GMT</pubDate>
      <description>Thanks to:&lt;br&gt;
http://davidhayden.com/blog/dave/archive/2006/05/28/2974.aspx&lt;br&gt;
&lt;br&gt;
Anon methods, delegates, predicates.&amp;nbsp; Anon methods are simple shortcuts so don't
have to declare a predicate method.&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;/span&gt;&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Collections.Generic; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Linq; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Text; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; TestAnonymousMethods
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; Program
{ &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
using an anon method&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; Mainx(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt;[]
args) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;[]
_integers &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;[]
evenIntegers &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; Array.FindAll(_integers, &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
this is the anonymous method below&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;delegate&lt;/span&gt;(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; integer)
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; (integer &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;%&lt;/span&gt; 2
== 0); } ); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;foreach&lt;/span&gt; (&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; integer &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;in&lt;/span&gt; _integers)
Console.WriteLine(integer); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;foreach&lt;/span&gt; (&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; integer &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;in&lt;/span&gt; evenIntegers)
Console.WriteLine(integer); } &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
not using anon method&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; Mainy(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt;[]
args) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;[]
_integers &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
passing in IsEven..a delegate which represents the IsEven method&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;[]
evenIntegers &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; Array.FindAll(_integers,
IsEven); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;foreach&lt;/span&gt; (&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; integer &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;in&lt;/span&gt; _integers)
Console.WriteLine(integer); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;foreach&lt;/span&gt; (&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; integer &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;in&lt;/span&gt; evenIntegers)
Console.WriteLine(integer); } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;bool&lt;/span&gt; IsEven(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; integer)
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; (integer &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;%&lt;/span&gt; 2
== 0); } &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
not using anon method and looking at predicate / delegate of Array.FindAll&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; Main(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt;[]
args) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;[]
_integers &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
a predicate is expected&lt;/span&gt; &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
passing in IsEven..a delegate which represents the IsEven method&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;[]
whatIsInBoth &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; Array.FindAll(_integers,
IsDaveSequence); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;foreach&lt;/span&gt; (&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; integer &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;in&lt;/span&gt; whatIsInBoth)
Console.WriteLine(integer); } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;bool&lt;/span&gt; IsDaveSequence(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; integer)
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;bool&lt;/span&gt; returnState &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;false&lt;/span&gt;; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;if&lt;/span&gt; ((integer
== 1) || (integer == 3) || (integer ==4)) returnState &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;true&lt;/span&gt;; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; returnState;
} } } &lt;/span&gt;&lt;/pre&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.programgood.net/aggbug.ashx?id=2b9daa70-1e45-4039-ac58-a4ee3d4c6225" /&gt;</description>
      <comments>http://www.programgood.net/CommentView,guid,2b9daa70-1e45-4039-ac58-a4ee3d4c6225.aspx</comments>
      <category>Events and Delegates</category>
      <category>Object Oriented Programming</category>
    </item>
  </channel>
</rss>