<?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>Generic Delegates and Anonymous Methods</title><link>http://weblogs.asp.net/cnagel/archive/2005/03/04/385077.aspx</link><description>Generics and anonymous methods are great improvements of C#. Having a generic collection persons of type List&amp;lt;Person&amp;gt;, the objects of the collection can be accessed with a simple foreach statement: foreach (Person p in persons) { Console.WriteLine</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: Generic Delegates and Anonymous Methods</title><link>http://weblogs.asp.net/cnagel/archive/2005/03/04/385077.aspx#7276189</link><pubDate>Fri, 11 Dec 2009 06:40:28 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7276189</guid><dc:creator>Rajesh</dc:creator><author>Rajesh</author><description>&lt;p&gt;public delegate T GenericDelegate&amp;lt;T&amp;gt;(T items );&lt;/p&gt;
&lt;p&gt;public void main()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;GenericTestClass obj = new GenericTestClass();&lt;/p&gt;
&lt;p&gt;GenericDelegate&amp;lt;int&amp;gt; delFunction= new GenericDelegate&amp;lt;int&amp;gt;(obj.IntTestFunct);&lt;/p&gt;
&lt;p&gt;Int intResult =delFunction(12);&lt;/p&gt;
&lt;p&gt;GenericDelegate&amp;lt; string &amp;gt; delFunction= new GenericDelegate&amp;lt; string &amp;gt;(obj.strTestFunct);&lt;/p&gt;
&lt;p&gt;string strResult =delFunction(“Hello”);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public GenericTestClass &lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;public static int IntTestFunc(int count )&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; return count;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Public static string &amp;nbsp;strTestFunc(string str)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; return str;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7276189" width="1" height="1"&gt;</description></item><item><title>Moms work from home.</title><link>http://weblogs.asp.net/cnagel/archive/2005/03/04/385077.aspx#6725136</link><pubDate>Fri, 07 Nov 2008 10:58:12 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6725136</guid><dc:creator>Work from home moms.</dc:creator><author>Work from home moms.</author><description>&lt;p&gt;Site build it work at home moms wahm. Moms work at home. Work at home moms resource wahmpros.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6725136" width="1" height="1"&gt;</description></item><item><title>re: Generic Delegates and Anonymous Methods</title><link>http://weblogs.asp.net/cnagel/archive/2005/03/04/385077.aspx#6709249</link><pubDate>Wed, 29 Oct 2008 04:06:56 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6709249</guid><dc:creator>Shahid Riaz Bhatti</dc:creator><author>Shahid Riaz Bhatti</author><description>&lt;p&gt;@ Tomas Scheel &lt;/p&gt;
&lt;p&gt;your solution is good, but the example which u gave later i.e. like this one &amp;quot;persons.Exists(p =&amp;gt; p.FirstName == &amp;quot;Bob&amp;quot;);&amp;quot; can not be implemented in .Net 2.0. &lt;/p&gt;
&lt;p&gt;The original post is excellent post if you are working in .Net 2.0 environment, the reason is that I have seen many developers using loops instead of using delegates..&lt;/p&gt;
&lt;p&gt;Tomas your example is good but only if some one is working in 3.5 environment, because lambdas expression are not supported in the earlier version..&lt;/p&gt;
&lt;p&gt;but at the end I must say that I like this post, it was really helpfull.&lt;/p&gt;
&lt;p&gt;Thanx for sharing&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6709249" width="1" height="1"&gt;</description></item><item><title>re: Generic Delegates and Anonymous Methods</title><link>http://weblogs.asp.net/cnagel/archive/2005/03/04/385077.aspx#5505835</link><pubDate>Thu, 27 Dec 2007 00:21:13 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5505835</guid><dc:creator>Tomas Scheel</dc:creator><author>Tomas Scheel</author><description>&lt;p&gt;@ A Skeptic&lt;/p&gt;
&lt;p&gt;This is a fairly simple example, but with .NET 3.5 it gets simpler... The last bit:&lt;/p&gt;
&lt;p&gt;persons.ForEach(delegate(Person p)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; Console.WriteLine(p);&lt;/p&gt;
&lt;p&gt;});&lt;/p&gt;
&lt;p&gt;Can be rewritten as&lt;/p&gt;
&lt;p&gt;persons.ForEach(p =&amp;gt; Console.WriteLine(p));&lt;/p&gt;
&lt;p&gt;I think that is far easier on code maintainability. In addition, it looks a lot cleaner. I like small bits like what is above, but with everything else, only in moderation. Something like:&lt;/p&gt;
&lt;p&gt;persons.ForEach(p =&amp;gt; &lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;// 30 lines later....&lt;/p&gt;
&lt;p&gt;});&lt;/p&gt;
&lt;p&gt;Is generally a bad idea.&lt;/p&gt;
&lt;p&gt;I especially like this for Find and Exists, for example:&lt;/p&gt;
&lt;p&gt;persons.Exists(p =&amp;gt; p.FirstName == &amp;quot;Bob&amp;quot;);&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5505835" width="1" height="1"&gt;</description></item><item><title>re: Generic Delegates and Anonymous Methods</title><link>http://weblogs.asp.net/cnagel/archive/2005/03/04/385077.aspx#5504864</link><pubDate>Wed, 26 Dec 2007 17:00:25 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5504864</guid><dc:creator>A Skeptic</dc:creator><author>A Skeptic</author><description>&lt;p&gt;Combining generics with delegates and then adding anonymous methods on top of it does a real number on the maintainability of the code. What do you think? I think this code is very hard to understand.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5504864" width="1" height="1"&gt;</description></item><item><title>Generics - Is there more than &amp;quot;Generic Collections&amp;quot;</title><link>http://weblogs.asp.net/cnagel/archive/2005/03/04/385077.aspx#406240</link><pubDate>Mon, 09 May 2005 08:24:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:406240</guid><dc:creator>TrackBack</dc:creator><author>TrackBack</author><description>I am currently preparing my developer track talks for the local Big&amp;amp;gt;Days 2005 road show and - oh what...&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=406240" width="1" height="1"&gt;</description></item><item><title>Generics and anonymous methods</title><link>http://weblogs.asp.net/cnagel/archive/2005/03/04/385077.aspx#404823</link><pubDate>Wed, 27 Apr 2005 14:35:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:404823</guid><dc:creator>TrackBack</dc:creator><author>TrackBack</author><description>&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=404823" width="1" height="1"&gt;</description></item><item><title>C# 2.0 and C  /CLI Event</title><link>http://weblogs.asp.net/cnagel/archive/2005/03/04/385077.aspx#403481</link><pubDate>Wed, 20 Apr 2005 01:58:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:403481</guid><dc:creator>TrackBack</dc:creator><author>TrackBack</author><description>&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=403481" width="1" height="1"&gt;</description></item><item><title>Geek Notes 2005-03-11</title><link>http://weblogs.asp.net/cnagel/archive/2005/03/04/385077.aspx#394349</link><pubDate>Fri, 11 Mar 2005 23:34:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:394349</guid><dc:creator>TrackBack</dc:creator><author>TrackBack</author><description>&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=394349" width="1" height="1"&gt;</description></item><item><title>Generic Methods</title><link>http://weblogs.asp.net/cnagel/archive/2005/03/04/385077.aspx#386157</link><pubDate>Sun, 06 Mar 2005 09:52:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:386157</guid><dc:creator>TrackBack</dc:creator><author>TrackBack</author><description>&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=386157" width="1" height="1"&gt;</description></item></channel></rss>