<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://dotnetslackers.com/Community/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>SimoneB's Blog : C#</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx</link><description>Tags: C#</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 SP1 (Build: 30415.43)</generator><item><title>CS2 - a personal source code search engine</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/09/13/CS2-_2D00_-a-personal-source-code-search-engine.aspx</link><pubDate>Thu, 13 Sep 2007 15:16:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:1806</guid><dc:creator>simoneb</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=1806</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=1806</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/09/13/CS2-_2D00_-a-personal-source-code-search-engine.aspx#comments</comments><description>&lt;p&gt;As I &lt;a href="http://dotnetslackers.com/community/blogs/simoneb/archive/2007/06/29/Indexing-and-searching-source-code-with-Lucene.Net.aspx"&gt;blogged some time ago&lt;/a&gt; I have been developing a source code search engine called &lt;a href="http://code.google.com/p/cs2project/"&gt;CS2&lt;/a&gt;. I completed it lately and, although it&amp;#39;s not as feature-rich as others - say, &lt;a href="http://www.koders.com/corp/products/pro/"&gt;Koders Pro Edition&lt;/a&gt; - it works pretty fine and suites the needs of whomever needs to search through code stored on its machine. Actually, it&amp;#39;s neither as heavy as Koders Pro as well, and plus it&amp;#39;s free and open source.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;It works as an ASP.NET 2.0 website and lets you request indexing folders and singular files, as well as search through them with a simple syntax. By default searches are performed in full-text fashion, but you can restrict queries on particular fields. The syntax is documented in the website. It&amp;#39;s multithreaded so indexing and searches can occur at the same time.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;It can be customized using the Properties.config file in the Configuration directory, by setting the folder where the files of the index are stored as well as the update interval. It&amp;#39;s capable of automatically removing from the index files which no longer exist on the file system as well as update those which have been edited.&lt;/p&gt;
&lt;p&gt;At the moment it can parse and index only C# source code but it&amp;#39;s written upon Windsor inversion of control container and can be easily extended just by adding a new parser. If anyone&amp;#39;s interested I will post about the complete process of adding a new parser.&lt;/p&gt;
&lt;p&gt;To get started just download the zipped website from the project homepage and run it. Then type the path of a folder containing C# source code files on your machine in the corresponding input field and wait some time for the indexing process to complete (you won&amp;#39;t see the progress since it occurs on another thread so that you can perform searches concurrently with indexing). Its duration depends upon the number of files to be parsed and indexed. Once indexing is complete updating the page shows the number of documents the index contains. Click on the link on the upper-right edge of the page to show the search syntax.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/09/13/CS2-_2D00_-a-personal-source-code-search-engine.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/09/13/CS2-_2D00_-a-personal-source-code-search-engine.aspx" /&gt;&lt;/a&gt;
&lt;/p&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=1806" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/IoC/default.aspx">IoC</category></item><item><title>New SQLite provider for ELMAH</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/09/06/New-SQLite-provider-for-ELMAH.aspx</link><pubDate>Wed, 05 Sep 2007 22:05:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:1778</guid><dc:creator>simoneb</dc:creator><slash:comments>15</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=1778</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=1778</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/09/06/New-SQLite-provider-for-ELMAH.aspx#comments</comments><description>&lt;p&gt;I bet you know what ELMAH is, don&amp;#39;t you? ELMAH stands for Error Logging Modules and Handlers, and its main and only purpose is to log exceptions occurring in ASP.NET web applications, within which it integrates with just a couple of entries in the Web.config file. It&amp;#39;s got a lot of documentation and it&amp;#39;s open source, so for any additional information check out &lt;a href="http://code.google.com/p/elmah/"&gt;its website&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Until yesterday it let you log exceptions in memory, on xml files and SQL Server, but a highly requested feature was to use &lt;a href="http://www.sqlite.org"&gt;SQLite&lt;/a&gt; as a backing store. Although I didn&amp;#39;t know SQLite much it has a cool &lt;a href="http://sourceforge.net/projects/sqlite-dotnet2/"&gt;.NET wrapper&lt;/a&gt; published on Sourceforge which exposes its features to the managed world, so I took on the &lt;a href="http://code.google.com/p/elmah/issues/detail?id=25"&gt;reported feature request&lt;/a&gt; and implemented it. SQLite doesn&amp;#39;t offer all of the features provided by SQL Server, but it&amp;#39;s almost completely SQL92 compliant so it has all that&amp;#39;s needed, and much more.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;So why to use SQLite instead of SQL Server or xml files? SQLite is a relational database engine, so it&amp;#39;s much faster than xml files, and compared with SQL Server at the moment our tests didn&amp;#39;t show any performance penalty; plus, it&amp;#39;s free! Paging through about 15.000 stored exceptions via ELMAH user interface resulted to be lightning fast!&lt;/p&gt;
&lt;p&gt;Since SQLite syntax isn&amp;#39;t widely documented, if anyone&amp;#39;s interested I will blog about it and the steps I went through to integrate it with ELMAH. Otherwise, the source code is available on &lt;a href="http://elmah.googlecode.com/svn/trunk/"&gt;ELMAH Subversion repository&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/09/06/New-SQLite-provider-for-ELMAH.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/09/06/New-SQLite-provider-for-ELMAH.aspx" /&gt;&lt;/a&gt;
&lt;/p&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=1778" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/Tools/default.aspx">Tools</category></item><item><title>IoC with Windsor Container part IV live</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/08/30/IoC-with-Windsor-Container-part-IV-live.aspx</link><pubDate>Thu, 30 Aug 2007 06:54:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:1764</guid><dc:creator>simoneb</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=1764</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=1764</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/08/30/IoC-with-Windsor-Container-part-IV-live.aspx#comments</comments><description>&lt;p&gt;The fourth and last article of the series about IoC is online and available for reading &lt;a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart4.aspx"&gt;here&lt;/a&gt;. Any feedback is welcome, and please rate the article! Each article links the other articles of the series so the reading should be straight from beginning to end.&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart4.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart4.aspx" /&gt;&lt;/a&gt;
&lt;/p&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=1764" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/IoC/default.aspx">IoC</category></item><item><title>IoC with Windsor Container part III live</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/08/11/IoC-with-Windsor-Container-part-III-live.aspx</link><pubDate>Sat, 11 Aug 2007 11:17:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:1716</guid><dc:creator>simoneb</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=1716</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=1716</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/08/11/IoC-with-Windsor-Container-part-III-live.aspx#comments</comments><description>&lt;p&gt;The third article of the series about IoC is online and available for reading &lt;a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart3.aspx"&gt;here&lt;/a&gt;. Any feedback is welcome, and please rate the article!
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart3.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart3.aspx" /&gt;&lt;/a&gt;
&lt;/p&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=1716" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/IoC/default.aspx">IoC</category></item><item><title>IoC with Windsor Container part II live</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/07/31/IoC-with-Windsor-Container-part-II-live.aspx</link><pubDate>Tue, 31 Jul 2007 15:45:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:1675</guid><dc:creator>simoneb</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=1675</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=1675</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/07/31/IoC-with-Windsor-Container-part-II-live.aspx#comments</comments><description>&lt;p&gt;The second article of the series about IoC is online and available for reading &lt;a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart2.aspx"&gt;here&lt;/a&gt;. Any feedback is welcome, and please rate the article!
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart2.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart2.aspx" /&gt;&lt;/a&gt;
&lt;/p&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=1675" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/IoC/default.aspx">IoC</category></item><item><title>IoC with Windsor Container on DotNetSlackers</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/07/24/IoC-with-Windsor-Container-on-DotNetSlackers.aspx</link><pubDate>Tue, 24 Jul 2007 09:41:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:1637</guid><dc:creator>simoneb</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=1637</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=1637</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/07/24/IoC-with-Windsor-Container-on-DotNetSlackers.aspx#comments</comments><description>
&lt;p&gt;I&amp;#39;ve written a 4 parts article series about Inversion of Control and Dependency Injection; the first part is already online &lt;a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx"&gt;here&lt;/a&gt;, the following parts should be online soon. It&amp;#39;s a comprehensive overview of most of the features offered by &lt;a href="http://www.castleproject.org/container/index.html"&gt;Castle Windsor Container&lt;/a&gt;, starting from the basics and on to more advanced topics. &lt;/p&gt;
&lt;p&gt;I hope you&amp;#39;ll enjoy, so feel free to leave feedback!&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx" /&gt;&lt;/a&gt;
&lt;/p&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=1637" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/IoC/default.aspx">IoC</category></item><item><title>Indexing and searching source code with Lucene.Net</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/06/29/Indexing-and-searching-source-code-with-Lucene.Net.aspx</link><pubDate>Fri, 29 Jun 2007 13:09:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:1538</guid><dc:creator>simoneb</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=1538</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=1538</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/06/29/Indexing-and-searching-source-code-with-Lucene.Net.aspx#comments</comments><description>&lt;p&gt;During the past week I&amp;#39;ve been working on a university project for my course of Information Retrieval, and although the project proposal from the teacher was to implement an xml parsing application written in Java I thought I would put in use the skills I developed on my own with the .NET framework to implement something more useful.&lt;/p&gt;

&lt;p&gt;My idea was to create a homemade source code indexing and search service, so I started fiddling with &lt;a href="http://incubator.apache.org/lucene.net/"&gt;Lucene.Net&lt;/a&gt;, &lt;a href="http://www.castleproject.org/"&gt;CastleProject&lt;/a&gt;, &lt;a href="http://www.codeplex.com/csparser"&gt;C# Parser&lt;/a&gt; and a couple other open source projects to see what I could come up with. There are already a lot of services which allows to search source code online, see &lt;a href="http://www.krugle.com/"&gt;Krugle&lt;/a&gt;, &lt;a href="http://www.google.com/codesearch"&gt;Google Code Search&lt;/a&gt; and &lt;a href="http://www.koders.com/"&gt;Koders&lt;/a&gt; among others.&lt;/p&gt;

&lt;p&gt;Well, of course I couldn&amp;#39;t use one of them as my course project, so I started implementing my own. I called it CS2 - C Sharp Code Search, and its source code is available under the MIT license on its &lt;a href="http://code.google.com/p/cs2project/"&gt;Google Project Hosting website&lt;/a&gt;. I think it&amp;#39;s a good example of the usage of Lucene.Net and CastleProject&amp;#39;s IoC container in a wanna be real life project.&lt;/p&gt;

&lt;p&gt;At the moment only the indexing part is implemented and you can see it working launching the console application project contained in the solution. The index created is compatible with the Lucene family implementations, so it can be browsed using an application like &lt;a href="http://www.getopt.org/luke/"&gt;Luke&lt;/a&gt;, until I implement the searching part.&lt;/p&gt;

&lt;p&gt;At the moment the features it implements are indexing C# source code files by parsing them and retrieving information like class, method and property names so that they can be searched against, as well as full-text search. It is extensible by implementing parsers for other languages, I&amp;#39;ve built it to make it pretty straightforward. It remembers the files indexed and periodically checks for modifications or file deletions. The console project comes with a full logging mechanism which shows what the program is actually doing. It is highly configurable via configuration files, see App.config and the files in the Configuration directory, used mostly for Castle Windsor configuration.&lt;br /&gt;
&lt;/p&gt;

&lt;p&gt;Let me know what you think! I&amp;#39;ll say more about it in the next weeks... and please, don&amp;#39;t hand it to your Information Retrieval teachers until I&amp;#39;ve delivered it to mine ;)&lt;br /&gt;
&lt;/p&gt;

&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/06/29/Indexing-and-searching-source-code-with-Lucene.Net.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/06/29/Indexing-and-searching-source-code-with-Lucene.Net.aspx" /&gt;&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=1538" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>How to sort a generic List&lt;T&gt;</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/06/20/How-to-sort-a-generic-List_3C00_T_3E00_.aspx</link><pubDate>Tue, 19 Jun 2007 23:19:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:1501</guid><dc:creator>simoneb</dc:creator><slash:comments>32</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=1501</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=1501</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/06/20/How-to-sort-a-generic-List_3C00_T_3E00_.aspx#comments</comments><description>&lt;p&gt;After reading &lt;a href="http://aspadvice.com/blogs/ssmith/archive/2007/06/17/Sort-Generic-List-of-T.aspx"&gt;this post&lt;/a&gt; from &lt;a href="http://aspadvice.com/blogs/ssmith/"&gt;Steven Smith&lt;/a&gt; I thought I should write something about it. &lt;/p&gt;

&lt;p&gt;Sorting a generic &lt;a href="http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx"&gt;List&amp;lt;T&amp;gt;&lt;/a&gt; is pretty straightforward if you know how to do it. With C# 2.0, anonymous methods come at hand, as well as the little known &lt;a href="http://msdn2.microsoft.com/en-us/library/tfakywbh.aspx"&gt;Comparison&amp;lt;T&amp;gt;&lt;/a&gt; delegate (check out &lt;a href="http://dotnetslackers.com/community/blogs/simoneb/archive/2006/08/20/367.aspx"&gt;this post&lt;/a&gt; for more information about this class as well as other useful classes new to C# 2.0).&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Ok, let&amp;#39;s suppose we have a product class (let me save some space by using C# 3.0 syntax).&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Product&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; ProductID { get; set; }&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ProductName { get; set; }&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;decimal&lt;/span&gt; UnitPrice { get; set; }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;When we have a list of products we may want to sort it on the ProductName property before displaying it to the user. This can be accomplished with the Sort method of the List&amp;lt;T&amp;gt; class, which defines several overloads. The most handy in this case is the Sort(Comparison&amp;lt;Product&amp;gt;) method and the result is easily achieved with a couple lines of code.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;List&amp;lt;Product&amp;gt; products = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Product&amp;gt;();&lt;br /&gt;&lt;br /&gt;products.Sort(&lt;span class="kwrd"&gt;delegate&lt;/span&gt;(Product p1, Product p2)&lt;br /&gt;              {&lt;br /&gt;                  &lt;span class="kwrd"&gt;return&lt;/span&gt; p1.ProductName.CompareTo(p2.ProductName);&lt;br /&gt;              });&lt;/pre&gt;

&lt;p&gt;So far so good, but what if we need to sort our list in several places during the execution of our program? Do we have to write that code each time? Actually no, since we can use the parameterless Sort() method of our list class. What this method does is use the &amp;quot;default comparer&amp;quot; to sort the list. So what&amp;#39;s this default comparer? It&amp;#39;s the comparer that&amp;#39;s automatically created if we implement the IComparable&amp;lt;T&amp;gt; interface. This way we can centralize the sorting logic into our class, and just call the parameterless Sort() method on it whenever we need it sorted on the ProductName property.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Product : IComparable&amp;lt;Product&amp;gt;&lt;br /&gt;{&lt;br /&gt;    [...]&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; CompareTo(Product other)&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; ProductName.CompareTo(other.ProductName);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;Ok, now what if we want to be able to sort it on the other two properties, ProductID and UnitPrice? Do we have to write an anonymous method each time as we did in the beginning? Of course no, since there&amp;#39;s a useful trick which prevents us from needing to do that. We can define two static Comparer&amp;lt;Product&amp;gt; properties in our product class, and supply them as parameters to the Sort(Comparer&amp;lt;T&amp;gt;) method of our list whenever we need it sorted on something which is not the default sorting logic.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Product : IComparable&amp;lt;Product&amp;gt;&lt;br /&gt;{&lt;br /&gt;    [...]&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Comparison&amp;lt;Product&amp;gt; PriceComparison =&lt;br /&gt;        &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(Product p1, Product p2)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; p1.Price.CompareTo(p2.Price);&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Comparison&amp;lt;Product&amp;gt; IDComparison =&lt;br /&gt;        &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(Product p1, Product p2)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; p1.ProductID.CompareTo(p2.ProductID);&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;    [...]&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;Since they are static they can be used simply like so: products.Sort(Product.PriceComparison) or products.Sort(Product.IDComparison), which will respectively sort the list by price and id. &lt;/p&gt;&lt;p&gt;Below is the full code of the Product class.&lt;br /&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Product : IComparable&amp;lt;Product&amp;gt;&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; id;&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; prodName;&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;decimal&lt;/span&gt; price;&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Comparison&amp;lt;Product&amp;gt; PriceComparison = &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(Product p1, Product p2)&lt;br /&gt;                                                        {&lt;br /&gt;                                                            &lt;span class="kwrd"&gt;return&lt;/span&gt; p1.price.CompareTo(p2.price);&lt;br /&gt;                                                        };&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Comparison&amp;lt;Product&amp;gt; IDComparison = &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(Product p1, Product p2)&lt;br /&gt;                                                     {&lt;br /&gt;                                                         &lt;span class="kwrd"&gt;return&lt;/span&gt; p1.id.CompareTo(p2.id);&lt;br /&gt;                                                     };&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; ProductID&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; id; }&lt;br /&gt;        set { id = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ProductName&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; prodName; }&lt;br /&gt;        set { prodName = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;decimal&lt;/span&gt; UnitPrice&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; price; }&lt;br /&gt;        set { price = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Product(&lt;span class="kwrd"&gt;int&lt;/span&gt; id, &lt;span class="kwrd"&gt;string&lt;/span&gt; prodName, &lt;span class="kwrd"&gt;decimal&lt;/span&gt; price)&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.id = id;&lt;br /&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.prodName = prodName;&lt;br /&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.price = price;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="preproc"&gt;#region&lt;/span&gt; IComparable&amp;lt;Product&amp;gt; Members&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; CompareTo(Product other)&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; ProductName.CompareTo(other.ProductName);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="preproc"&gt;#endregion&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ToString()&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(&lt;span class="str"&gt;&amp;quot;Id: {0} Name: {1} Price: {2}&amp;quot;&lt;/span&gt;, id, prodName, price);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;br /&gt;

&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/06/20/How-to-sort-a-generic-List_3C00_T_3E00_.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/06/20/How-to-sort-a-generic-List_3C00_T_3E00_.aspx" /&gt;&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=1501" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Searching source code on Koders.com</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/05/22/Searching-source-code-on-Koders.com.aspx</link><pubDate>Mon, 21 May 2007 21:49:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:1384</guid><dc:creator>simoneb</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=1384</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=1384</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/05/22/Searching-source-code-on-Koders.com.aspx#comments</comments><description>&lt;p&gt;Lately &lt;a href="http://haacked.com/"&gt;Phil Haack&lt;/a&gt; has &lt;a href="http://haacked.com/archive/2007/05/18/searching-open-source-code-with-open-source.aspx"&gt;blogged&lt;/a&gt; about his new job at &lt;a href="http://www.koders.com/"&gt;Koders.com&lt;/a&gt;. As far as I can see they provide a search engine for open source code and let people submit their repositories so that their code can be indexed.&lt;/p&gt;

&lt;p&gt;I&amp;#39;ve developed some interest in code searching since I&amp;#39;m going to implement a code search engine for my Information Retrieval course here in Italy, and hope Phil will give me some ideas in his future blog posts about Koders. In fact, I&amp;#39;m going to use Lucene.NET for indexing the source files - which I think they are using, too - and maybe I can learn some good practices and get some tips from them.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Though it doesn&amp;#39;t have a slick interface as &lt;a href="http://www.krugle.com/"&gt;Krugle&lt;/a&gt;, Koders provides a service for querying its engine from the web, returning the search results as RSS, so I thought I could write a wrapper on top of it. I&amp;#39;ve come up with a fluent interface query class based on strongly typed classes generated with the &lt;a href="http://www.codeplex.com/ASPNETRSSToolkit"&gt;ASP.NET RSS Toolkit&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Koders service allows refining search results by programming language, license, logical placement of the code in the source file (class name, method name, interface name) and by file name.&lt;/p&gt;

&lt;p&gt;In the wrapper I created the querying is a two-step process. First you need to create a KodersQuery class supplying the parameters of the search, then you use one of the strongly typed classes generated by the &lt;a href="http://www.codeplex.com/ASPNETRSSToolkit"&gt;ASP.NET RSS Toolkit&lt;/a&gt; to load and parse the results:&lt;br /&gt;
&lt;/p&gt;

&lt;pre class="csharpcode"&gt;KodersQuery q = &lt;span class="kwrd"&gt;new&lt;/span&gt; KodersQuery()&lt;br /&gt;    .ProgrammingLanguage(ProgrammingLanguage.Csharp)&lt;br /&gt;    .SearchTerms(&lt;span class="str"&gt;&amp;quot;Subtext&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;KodersRss rss = KodersRss.Load(q);&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;The KodersRss class wraps the xml returned by the query and provides the items found via the KodersRss.Channel.Items property.&lt;/p&gt;

&lt;p&gt;Given the profileration of LINQ to X implementations it would be cool to create such an API for search source code, either on Google Code Search or Koders, but I&amp;#39;m still waiting for the final bits of LINQ before to attempt doing it. In fact I&amp;#39;ve seen the source code of some LINQ to X libraries and it looks like they won&amp;#39;t compile with Orcas if they were created with the May CTP.&lt;/p&gt;

&lt;p&gt;At the moment the source code is on my &lt;a href="https://busybox.svn.sourceforge.net/svnroot/busybox/trunk/KodersSearch/"&gt;svn repository&lt;/a&gt;.&lt;/p&gt;

&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/05/22/Searching-source-code-on-Koders.com.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/05/22/Searching-source-code-on-Koders.com.aspx" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=1384" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>When is a singleton not a singleton? Serialization!</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/05/04/When-is-a-singleton-not-a-singleton_3F00_-Serialization_2100_.aspx</link><pubDate>Thu, 03 May 2007 22:37:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:1288</guid><dc:creator>simoneb</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=1288</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=1288</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/05/04/When-is-a-singleton-not-a-singleton_3F00_-Serialization_2100_.aspx#comments</comments><description>&lt;p&gt;Some days ago I &lt;a href="http://dotnetslackers.com/community/blogs/simoneb/archive/2007/04/30/.NET-interview-question-_2D00_-when-is-a-singleton-not-a-singleton_3F00_.aspx"&gt;wrote&lt;/a&gt; about the issue of having multiple instances of a singleton class in the same AppDomain. As for the implementation I gave and as emerged from the feedback to the post, this can happen when that class is serialized and deserialized or when it is instantiated via reflection. This is the original implementation:&lt;br /&gt;
&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Singleton&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; Singleton instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; Singleton();&lt;br /&gt;&lt;br /&gt;    &lt;span class="rem"&gt;// Private constructor to prevent external instantiation&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; Singleton()&lt;br /&gt;    { }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton Instance&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; instance; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt; In case of serialization, there&amp;#39;s actually a workaround explicitly provided by the .NET framework. This consists in implementing the ISerializable interface to provide a custom serialization mechanism.&lt;/p&gt;

&lt;p&gt;Upon serialization, the GetObjectData method of the ISerializable interface is called to get the custom data needed for serializing the object. These data is set by setting up an appropriate SerializationInfo object. What&amp;#39;s needed is a way to instruct the formatter about the identity of the object to be serialized, so that when it&amp;#39;s deserialized it will be a reference to the same object - which we need to be unique. This can be accomplished by using the SetType method of the SerializationInfo object passed as a parameter to the GetObjectData method.&lt;br /&gt;
&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; GetObjectData(SerializationInfo info, StreamingContext context)&lt;br /&gt;{&lt;br /&gt;    info.SetType(...); // accepts a Type parameter&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;What&amp;#39;s the type we need to supply to the SetType method? It&amp;#39;s not typeof(Singleton) - no need to specify that though, it&amp;#39;s implicit - because in that case, upon deserialization, a particular constructor that the class has to define is called. The following is the signature of the constructor called by default when deserializing a class which implements the ISerializable interface:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;Singleton(SerializationInfo, StreamingContext);&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;If no type is set using the SerializationInfo.SetType method into the GetObjectData method (called upon serialization), the above constructor is called on deserialization to populate the object being deserialized with custom data. If such a constructor is not implemented a SerializationException exception is thrown.&lt;/p&gt;

&lt;p&gt;We won&amp;#39;t need to implement that constructor since we want to avoid instantiating the class, but instead we need to pass to the SerializationInfo.SetType method the type of a helper class capable of returning the right instance of our singleton class. Again, the framework provides an interface explicitly designed for this task, called IObjectReference.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; System.Runtime.Serialization&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IObjectReference&lt;br /&gt;    { &lt;br /&gt;        &lt;span class="kwrd"&gt;object&lt;/span&gt; GetRealObject(StreamingContext context);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;This interface lets you specify that the class implementing it doesn&amp;#39;t create new objects, but instead returns a reference to another object. This is good for us, since we can implement this interface by returning, via the GetRealObject method, a reference to the singleton instance of the Singleton class, thus preventing the formatter from instantiating a new object during deserialization. &lt;/p&gt;

&lt;p&gt;So our helper class ends up having an implementation like in the following code snippet:&amp;nbsp;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;[Serializable]&lt;br /&gt;&lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SingletonSerializationHelper : IObjectReference&lt;br /&gt;{&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; GetRealObject(StreamingContext context)&lt;br /&gt;   {&lt;br /&gt;       &lt;span class="kwrd"&gt;return&lt;/span&gt; Singleton.Instance;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;Going back to the Singleton class, the original code now is edited to make use of the SingletonSerialization helper class:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;[Serializable]&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Singleton : ISerializable&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; Singleton instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; Singleton();&lt;br /&gt;&lt;br /&gt;    &lt;span class="rem"&gt;// Private constructor to prevent external instantiation&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; Singleton()&lt;br /&gt;    { }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton Instance&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; instance; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; GetObjectData(SerializationInfo info, StreamingContext context)&lt;br /&gt;    {&lt;br /&gt;        info.SetType(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(SingletonSerializationHelper));&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;What this code does when an instance of the class is serialized and deserialized can be summarized as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Upon serialization, the GetObjectData method of the Singleton class is called and the formatter is instructed that the type of the object being serialized is actually not Singleton, but instead SingletonSerializationHelper.&lt;/li&gt;
&lt;li&gt;Upon deserialization, the formatter knows that it needs to instantiate an object of type SingletonSerializationHelper, this being a class implementing the IObjectReference interface. Therefore, instead of calling its constructor, the GetRealObjectMethod is called, and the right, singleton instance of the Singleton class is returned.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Again, note that if the SerializationInfo.SetType method wasn&amp;#39;t called during serialization, when deserializing the formatter would have tried to call the overloaded constructor of the Singleton class, and if it wasn&amp;#39;t provided a SerializationException exception would have been thrown. The reason why it isn&amp;#39;t called upon deserialization is that we make the formatter believe that the type of the class it will have to deserialize is of type SingletonSerializationHelper in place of Singleton.&lt;/p&gt;
&lt;p&gt;This example appears on the MSDN Library in a couple of pages about &lt;a href="http://msdn2.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx"&gt;serialization&lt;/a&gt;, although I learned about this workaround when reading an old &lt;a href="http://msdn.microsoft.com/msdnmag/issues/02/07/net/"&gt;article&lt;/a&gt; written by Jeffrey Ritcher on the MSDN Magazine.&amp;nbsp;&lt;/p&gt;

&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/05/04/When-is-a-singleton-not-a-singleton_3F00_-Serialization_2100_.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/05/04/When-is-a-singleton-not-a-singleton_3F00_-Serialization_2100_.aspx" /&gt;&lt;/a&gt;
&lt;br /&gt;&lt;br /&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=1288" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>.NET interview question - when is a singleton not a singleton?</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/04/30/.NET-interview-question-_2D00_-when-is-a-singleton-not-a-singleton_3F00_.aspx</link><pubDate>Sun, 29 Apr 2007 20:14:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:1269</guid><dc:creator>simoneb</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=1269</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=1269</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2007/04/30/.NET-interview-question-_2D00_-when-is-a-singleton-not-a-singleton_3F00_.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;ve read some posts lately with questions people use to ask or have been asked during a job interview, so I wanted to post a tricky question too.&lt;/p&gt;
&lt;p&gt; I&amp;#39;ve talked about singletons in the past, and what we use to think is that a classic singleton class has to be unique into a specific AppDomain. Let&amp;#39;s take this simple implementation of the singleton pattern, which is thread safe, too: [edit: made the class sealed to avoid confusion]&lt;br /&gt;
&lt;/p&gt;


&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Singleton&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; Singleton instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; Singleton();&lt;br /&gt;&lt;br /&gt;    &lt;span class="rem"&gt;// Private constructor to prevent external instantiation&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; Singleton()&lt;br /&gt;    {}&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton Instance&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; instance; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;There&amp;#39;s at least one situation where you can find yourself having multiple instances of the singleton class in the same AppDomain, can you tell when?&lt;/p&gt;

&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/04/30/.NET-interview-question-_2D00_-when-is-a-singleton-not-a-singleton_3F00_.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2007/04/30/.NET-interview-question-_2D00_-when-is-a-singleton-not-a-singleton_3F00_.aspx" /&gt;&lt;/a&gt;
&lt;br /&gt;&lt;br /&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=1269" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>The ASP.NET Singleton-per-Request pattern</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2006/08/21/The-ASP.NET-Singleton_2D00_per_2D00_Request-pattern.aspx</link><pubDate>Mon, 21 Aug 2006 04:58:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:382</guid><dc:creator>simoneb</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=382</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=382</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2006/08/21/The-ASP.NET-Singleton_2D00_per_2D00_Request-pattern.aspx#comments</comments><description>Lately I needed to have an instance of a class to be unique for a certain amount of time, that is, one instance only of that class should exist for that time interval in my app domain.&lt;br&gt;In these cases what comes to my mind is the &lt;a href="http://www.yoda.arachsys.com/csharp/singleton.html"&gt;Singleton Design Pattern&lt;/a&gt;, which imposes that one instance only of a class can be loaded into an app domain and is alive as long as the app domain is.&lt;br&gt;My problem was that I needed that object to be unique only for a small time interval, which later I identified as being the life of a single request. That's when I recalled of a very useful IDictionary collection whose lifetime corresponds to the request-response cycle, and where you can store items which are then available during all the stages of the pipeline. This object is the &lt;i&gt;Items&lt;/i&gt; instance property of the &lt;i&gt;HttpContext &lt;/i&gt;class, and is defined as following:


&lt;pre class="csharpcode"&gt;System.Collections.IDictionary HttpContext.Items&lt;/pre&gt;

Let me say it again: into this collection you can store items that are then made available during all the request-response process, and are unique to that particular request-response. To enfore this and make it even clearer, the description of this object as given by the intellisense is:&lt;br&gt;&lt;br&gt;&lt;i&gt;Gets a key/value collection that can be used to organize and share data between&lt;br&gt;an System.Web.IHttpModule interface and an System.Web.IHttpHandler interface&lt;br&gt;during an HTTP request.&lt;/i&gt;&lt;br&gt;&lt;br&gt;You understand how powerful this object is, and the ASP.NET engine uses this mechanism itself to mantain some state information, like the session id and something more.&lt;br&gt;Going back to my problem, what I wanted to achieve is a lazy load (created only when necessary) instance of a class being unique to my request, so I came up with this code.


&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SingletonPerRequest&lt;br&gt;{&lt;br&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; SingletonPerRequest Current&lt;br&gt;    {&lt;br&gt;        get&lt;br&gt;        {&lt;br&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; (HttpContext.Current.Items[&lt;span class="str"&gt;"SingletonPerRequest"&lt;/span&gt;] ??&lt;br&gt;                (HttpContext.Current.Items[&lt;span class="str"&gt;"SingletonPerRequest"&lt;/span&gt;] = &lt;br&gt;                &lt;span class="kwrd"&gt;new&lt;/span&gt; SingletonPerRequest())) &lt;span class="kwrd"&gt;as&lt;/span&gt; SingletonPerRequest;&lt;br&gt;&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;

which can be used calling the static &lt;i&gt;Current&lt;/i&gt; property of the class. What this code does is check if an instance of the class is already in the Items collection. In that case, it returns a reference to that instance, otherwise a new instance is created and placed in the Items collection, so that the next time it is requested during this same request it's already there.&lt;br&gt;Note that here we don't need any synchronization mechanism, since a single request is not executed &lt;b&gt;concurrently &lt;/b&gt;by more than one thread.
&lt;br&gt;&lt;br&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2006/08/21/382.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2006/08/21/382.aspx" alt="kick it on DotNetKicks.com" border="0"&gt;&lt;/a&gt;
&lt;br&gt;&lt;br&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=382" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Managing collections with functors</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2006/08/20/367.aspx</link><pubDate>Sat, 19 Aug 2006 21:22:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:367</guid><dc:creator>simoneb</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=367</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=367</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2006/08/20/367.aspx#comments</comments><description>Functors are a cool feature of .NET 2.0 which ease the management of collections. &lt;br&gt;&lt;a href="http://www.practicaldot.net"&gt;Patrick Smacchia&lt;/a&gt;, in the late 2004, has written &lt;a href="http://www.theserverside.net/tt/articles/showarticle.tss?id=AnonymousMethods"&gt;an interesting article&lt;/a&gt; for TheServerSide which covers many topics, and functors too. The recent advent of Linq will perhaps make them obsolete - even before they become famous! - but it is a feature to be aware of anyway.&lt;br&gt;Let me guide those of you who don't have much familiarity with delegates and anonymous methods through the topic.&lt;br&gt;Let's suppose we have a class which describes a student (I won't use properties just to save some space):


&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Student&lt;br&gt;{&lt;br&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; name;&lt;br&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; age;&lt;br&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; course;&lt;br&gt;&lt;br&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Student(&lt;span class="kwrd"&gt;string&lt;/span&gt; name, &lt;span class="kwrd"&gt;int&lt;/span&gt; age, &lt;span class="kwrd"&gt;string&lt;/span&gt; course)&lt;br&gt;    {&lt;br&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.name = name;&lt;br&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.age = age;&lt;br&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.course = course;&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;Then we create a collection of students which represents an entire school and populate it with some students:


&lt;pre class="csharpcode"&gt;List&amp;lt;Student&amp;gt; school = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Student&amp;gt;();&lt;br&gt;&lt;br&gt;school.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Student(&lt;span class="str"&gt;"Bart"&lt;/span&gt;, &lt;span class="str"&gt;14&lt;/span&gt;, &lt;span class="str"&gt;"Science"&lt;/span&gt;));&lt;br&gt;school.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Student(&lt;span class="str"&gt;"Lisa"&lt;/span&gt;, &lt;span class="str"&gt;13&lt;/span&gt;, &lt;span class="str"&gt;"Science"&lt;/span&gt;));&lt;br&gt;school.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Student(&lt;span class="str"&gt;"Milhouse"&lt;/span&gt;, &lt;span class="str"&gt;12&lt;/span&gt;, &lt;span class="str"&gt;"Science"&lt;/span&gt;));&lt;br&gt;school.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Student(&lt;span class="str"&gt;"Homer"&lt;/span&gt;, &lt;span class="str"&gt;45&lt;/span&gt;, &lt;span class="str"&gt;"Beer"&lt;/span&gt;));&lt;br&gt;&lt;/pre&gt;

After setting up the environment for testing functors, we are ready to go. Imagine we want to retrieve from our list only the students with certain characteristics, i.e. students which attend the science course, and put them inside another list. There are a couple of ways of doing that, and maybe the one which first comes to our mind is creating a method like the following:


&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;// retrieves all the students of the science course&lt;/span&gt;
&lt;span class="kwrd"&gt;public static&lt;/span&gt; List&amp;lt;Student&amp;gt; GetScienceStudents(List&amp;lt;Student&amp;gt; allStudents)&lt;br&gt;{&lt;br&gt;    List&amp;lt;Student&amp;gt; scienceStudents = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Student&amp;gt;();&lt;br&gt;&lt;br&gt;    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Student student &lt;span class="kwrd"&gt;in&lt;/span&gt; allStudents)&lt;br&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (student.course == &lt;span class="str"&gt;"Science"&lt;/span&gt;)&lt;br&gt;            scienceStudents.Add(student);&lt;br&gt;&lt;br&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; scienceStudents;&lt;br&gt;}&lt;/pre&gt;

which we can call in this way:


&lt;pre class="csharpcode"&gt;List&amp;lt;Student&amp;gt; scienceClass1 = GetScienceStudents(school);&lt;/pre&gt;

As you can see, that's a lot of code for such a simple task, we can do better! 
If you play with the intellisense on the school list, you can see that there are some methods which accept strange parameters, in particular there's the following method which looks interesting in our case:


&lt;pre class="csharpcode"&gt;List&amp;lt;Student&amp;gt; List&amp;lt;Student&amp;gt;.FindAll(Predicate&amp;lt;Student&amp;gt; match)&lt;/pre&gt;

It looks promising because it returns a list of students, that's exactly what we want. So, what's that Predicate&amp;lt;Student&amp;gt; that it accepts as a parameter? It's a delegate, that is, a pointer to a method, which defines the signature of the method. This delegate is new to C# 2.0, along with other three (which I will describe later):


&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Action&amp;lt;T&amp;gt;(T obj);&lt;br&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; TOutput Converter&amp;lt;TInput, TOutput&amp;gt;(TInput input);&lt;br&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Comparison&amp;lt;T&amp;gt;(T x, T y);&lt;br&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; Predicate&amp;lt;T&amp;gt;(T obj);&lt;/pre&gt;&lt;font size="4"&gt;The Predicate&amp;lt;T&amp;gt; delegate&lt;/font&gt;&lt;br&gt;Let's focus on the Predicate&amp;lt;T&amp;gt; delegate, because once you understand this, the others are immediate. Since the method FindAll of our list accepts such a delegate, it means that it accepts any method with the same signature. So let's write a method faithful to that signature which returns true if the supplied student attends the science course:


&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;// returns true if the student attends the science course&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsScienceStudent(Student student)&lt;br&gt;{&lt;br&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; student.course == &lt;span class="str"&gt;"Science"&lt;/span&gt;;&lt;br&gt;}&lt;/pre&gt;

Having this method, now we can call the FindAll method in this way:


&lt;pre class="csharpcode"&gt;List&amp;lt;Student&amp;gt; scienceClass2 = school.FindAll(&lt;span class="kwrd"&gt;new&lt;/span&gt; Predicate&amp;lt;Student&amp;gt;(IsScienceStudent));&lt;br&gt;&lt;/pre&gt;

Even in this case C# 2.0 makes our life easier because we don't need to create explicitly an instance of the Predicate delegate, but we can write the following, and the compiler takes care of doing that for us (in fact, the compiler emits the same code for both the previous and following method call):


&lt;pre class="csharpcode"&gt;List&amp;lt;Student&amp;gt; scienceClass3 = school.FindAll(IsScienceStudent);&lt;/pre&gt;

Ok, that's something better compared to that first verbose method, but we still have to create a method just to retrieve the students of the science class. &lt;br&gt;Here come the C# 2.0 anonymous methods. Using this new feature, we don't even need to write the &lt;i&gt;IsScienceStudent &lt;/i&gt;method, because we can supply its body directly when calling the &lt;i&gt;FindAll &lt;/i&gt;method:


&lt;pre class="csharpcode"&gt;List&amp;lt;Student&amp;gt; scienceClass = &lt;br&gt;    school.FindAll(&lt;span class="kwrd"&gt;delegate&lt;/span&gt;(Student student)&lt;br&gt;                   {&lt;br&gt;                       &lt;span class="kwrd"&gt;return&lt;/span&gt; student.course == &lt;span class="str"&gt;"Science"&lt;/span&gt;;&lt;br&gt;                   });&lt;br&gt;&lt;/pre&gt;This is an anonymous method, because we are creating a method by supplying his body yet without giving it a name. I'm not delving here into the topic of anonymous methods, but it's interesting to see what is happening behind the scenes, that is, what the compiler does when we declare and use an anonymous method. Using an IL disassembler like Reflector helps very much during this task. Note that the names of the members generated by the compiler may vary and the members themselves are decorated with the &lt;i&gt;CompilerGenerated &lt;/i&gt;attribute. I'm reporting here the exact output I got when disassembling the sample program I created:&lt;br&gt;&lt;ol&gt;
&lt;li&gt;A private static delegate object of type Predicate&amp;lt;Student&amp;gt; is created


&lt;pre class="csharpcode"&gt;[CompilerGenerated]&lt;br&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Predicate&amp;lt;Student&amp;gt; &amp;lt;&amp;gt;9__CachedAnonymousMethodDelegate4;&lt;br&gt;&lt;/pre&gt;


&lt;/li&gt;
&lt;li&gt;A private static method faithful to the signature imposed by the Predicate&amp;lt;Student&amp;gt; delegate is created (notice that this method is the same as the IsScienceStudent method we created before!)


&lt;pre class="csharpcode"&gt;[CompilerGenerated]&lt;br&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; &amp;lt;Main&amp;gt;b__0(Student student)&lt;br&gt;{&lt;br&gt;      &lt;span class="kwrd"&gt;return&lt;/span&gt; (student.course == &lt;span class="str"&gt;"Science"&lt;/span&gt;);&lt;br&gt;}&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
During the execution of the program, a check is performed to see whether an instance of our delegate already exists, and in case it doesn't, a new one is created, pointing to the method created in the step above (that's why the compiler called it something like CachedAnonymousDelegate, because it's static and only one instance of it is created at runtime):


&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (Program.&amp;lt;&amp;gt;9__CachedAnonymousMethodDelegate4 == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br&gt;{&lt;br&gt;    Program.&amp;lt;&amp;gt;9__CachedAnonymousMethodDelegate4 = &lt;br&gt;        &lt;span class="kwrd"&gt;new&lt;/span&gt; Predicate&amp;lt;Student&amp;gt;(Program.&amp;lt;Main&amp;gt;b__0);&lt;br&gt;}&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
Finally, the FindAll method is called supplying our cached anonymous delegate as a parameter:


&lt;pre class="csharpcode"&gt;List&amp;lt;Student&amp;gt; list5 = list1.FindAll(Program.&amp;lt;&amp;gt;9__CachedAnonymousMethodDelegate4);&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
As you can see, the compiler goes through all the steps we did before we knew about anonymous methods, thus giving us a great shortcut when writing code.&lt;br&gt;&lt;br&gt;Going back to the primary topic, this is what functors are, they are parametric routines. In this case, the functor was the delegate instance created and used dinamically by the call to the FindAll method, which in turn parametrized it by calling it each time with a different input parameter, and not just in the case of an anonymous method, but even when we used the IsScienceStudent method.&lt;br&gt;As you can see, then, the Predicate&amp;lt;T&amp;gt; delegate is useful when we want to get a list of items which satisfy some conditions.&lt;br&gt;&lt;br&gt;Now that you've assimilated the notion of functor we can see how the other delegates anticipated before can be useful. I won't show it from scratch using a method and then a delegate, but I will go directly to the point using anonymous methods.&lt;br&gt;&lt;br&gt;&lt;font size="4"&gt;The Action&amp;lt;T&amp;gt; delegate&lt;/font&gt;&lt;br&gt;This class is useful when you want to perform some task on all the items of the collection. Thus, le'ts suppose that, once obtained the students of the science course using one of the approaches described before, we want to change the name of the course for each of them, because the next year the science course will be called "Natural Science". Using an anonymous method with the Action&amp;lt;T&amp;gt; delegate signature this can be done using the ForEach method of our list:


&lt;pre class="csharpcode"&gt;scienceClass.ForEach(&lt;span class="kwrd"&gt;delegate&lt;/span&gt;(Student student) &lt;br&gt;                      { &lt;br&gt;                          student.course = &lt;span class="str"&gt;"Natural Science"&lt;/span&gt;; &lt;br&gt;                      });&lt;br&gt;&lt;/pre&gt;

After invoking this method all the students contained in the scienceClass list will have the course field equal to "Natural Science".&lt;br&gt;&lt;br&gt;&lt;font size="4"&gt;

The Comparison&amp;lt;T&amp;gt; delegate&lt;/font&gt;&lt;br&gt;This delegate class is useful when we want to compare two objects of the same type, and in particular when the comparison is a custom comparison, that is, on a complex type like a class. This delegate can be used for sorting operations. In fact, the List&amp;lt;T&amp;gt; class exposes a method called Sort which accepts, among others, an instance of the Comparison&amp;lt;T&amp;gt; delegate. Thus, let's suppose that we want to sort the students of the natural science class based on the age (in ascending order); we can write the following code:


&lt;pre class="csharpcode"&gt;scienceClass.Sort(&lt;span class="kwrd"&gt;delegate&lt;/span&gt;(Student s1, Student s2) &lt;br&gt;                   { &lt;span class="kwrd"&gt;return&lt;/span&gt; s1.age - s2.age; });&lt;/pre&gt;

After this operation, the scienceClass list will contain the students sorted by ascending age.&lt;br&gt;&lt;br&gt;&lt;font size="4"&gt;The Converter&amp;lt;TInput, TOutput&amp;gt; delegate&lt;/font&gt;&lt;br&gt;This last delegate class is useful when we want to obtain a list of items of type TOutput from a list of items of type TInput. In our case, let's suppose that we want to obtain the list of the names of the students attending our famous natural science course. The TInput type will be the Student class, because we provide a list of students, and the TOutput type will be the string type, because we want in return a list of names. The method of the List&amp;lt;T&amp;gt; collection which is useful in this case is the ConvertAll method, which in fact accepts a Converter delegate:


&lt;pre class="csharpcode"&gt;List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; names = &lt;br&gt;    scienceClass.ConvertAll&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;(&lt;span class="kwrd"&gt;delegate&lt;/span&gt;(Student student)&lt;br&gt;                                     {&lt;br&gt;                                         &lt;span class="kwrd"&gt;return&lt;/span&gt; student.name;&lt;br&gt;                                     });&lt;br&gt;&lt;/pre&gt;

&lt;font size="4"&gt;Conclusion&lt;/font&gt;&lt;br&gt;At the end of the post you can find a zipped sample console application I've created while writing this post.&lt;br&gt;As a side note, you must be aware that only the List&amp;lt;T&amp;gt; and Array classes expose methods which accept these delegates as parameters.
&lt;br&gt;I hope this was useful and interesting. That said, for further investigation and for the complete list of methods which accept these delegates I point you to the &lt;a href="http://www.theserverside.net/tt/articles/showarticle.tss?id=AnonymousMethods"&gt;article&lt;/a&gt; (which has lately become an entire chapter on his recent book) by Patrick Smacchia.

&lt;br&gt;&lt;br&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2006/08/20/367.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://dotnetslackers.com/community/blogs/simoneb/archive/2006/08/20/367.aspx" alt="kick it on DotNetKicks.com" border="0"&gt;&lt;/a&gt;
&lt;br&gt;&lt;br&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=367" width="1" height="1"&gt;</description><enclosure url="http://dotnetslackers.com/Community/blogs/simoneb/attachment/367.ashx" length="2572" type="application/zip" /><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Lookup a control at an arbitrary level of the Page hierarchy</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2006/07/03/Lookup-a-control-at-an-arbitrary-level-of-the-Page-hierarchy.aspx</link><pubDate>Mon, 03 Jul 2006 16:45:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:145</guid><dc:creator>simoneb</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=145</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=145</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2006/07/03/Lookup-a-control-at-an-arbitrary-level-of-the-Page-hierarchy.aspx#comments</comments><description>In ASP.NET 2.0 the page hierarchy often happens to be deeper than it was with the older version, mostly when using MasterPages. Consequently, when you want to obtain a reference to a control of the page given its ID and you don't know at which level of the hierarchy it is placed, using the FindControl() method of the Page object isn't the right way of proceding since it just performs a search among the direct children of the page itself.&lt;br&gt;&lt;br&gt;In such cases you may use a recursive search, which walks all the page hierarchy. Note that you should use this workaround only when you strictly need it, because it could introduce a consistent overhead in the processing of the page due to the fact that everything becomes a control when on the server, and you'll find yourself looping through a considerable number of controls. This is a recursive implementation of the method:


&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Control FindControl(&lt;span class="kwrd"&gt;string&lt;/span&gt; controlId, ControlCollection controls)
{
      &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Control control &lt;span class="kwrd"&gt;in&lt;/span&gt; controls)
      {
            &lt;span class="kwrd"&gt;if&lt;/span&gt;(control.ID == controlId)
                  &lt;span class="kwrd"&gt;return&lt;/span&gt; control;

            &lt;span class="kwrd"&gt;if&lt;/span&gt;(control.HasControls())
            {
                  Control nestedControl = FindControl(controlId, control.Controls);

                  &lt;span class="kwrd"&gt;if&lt;/span&gt;(nestedControl != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
                        &lt;span class="kwrd"&gt;return&lt;/span&gt; nestedControl;
            }
      }
      &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;;
}&lt;/pre&gt;

Then you can simply use it this way:


&lt;pre class="csharpcode"&gt;Control foundControl = FindControl(controlToFindID, Page.Controls);&lt;/pre&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=145" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Template merging with NVelocity and ASP.NET</title><link>http://dotnetslackers.com/Community/blogs/simoneb/archive/2006/07/03/138.aspx</link><pubDate>Mon, 03 Jul 2006 15:46:00 GMT</pubDate><guid isPermaLink="false">6afe0437-14b4-41d5-bc66-6d54a24dbd48:138</guid><dc:creator>simoneb</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/rsscomments.aspx?PostID=138</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetslackers.com/Community/blogs/simoneb/commentapi.aspx?PostID=138</wfw:comment><comments>http://dotnetslackers.com/Community/blogs/simoneb/archive/2006/07/03/138.aspx#comments</comments><description>&lt;p&gt;Some time ago I wrote an article for &lt;a href="http://codeproject.com/"&gt;CodeProject&lt;/a&gt; about merging templates with &lt;a href="http://nvelocity.sourceforge.net/"&gt;NVelocity&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For those who don't know what template merging means, think about a
newsletter management system which needs to send emails to website
users.&lt;br&gt;Website admins may need to customize the messages according
to user info, that is, greeting the user with a "Hello [username]" or
something like that, and have message templates easy to mantain, maybe
keeping them on the file system as html pages or creating them
dinamically in the code of the application.&lt;/p&gt;
&lt;p&gt;How would you do this by hand? Don't think too much, use NVelocity!&lt;/p&gt;
&lt;p&gt;In the article I develop a library which greatly eases the use of
NVelocity in ASP.NET applications, as well as in any other type of .NET
projects.&lt;/p&gt;
&lt;p&gt;You can read the article as well as download source code and demo files here: &lt;a href="http://codeproject.com/useritems/nvelocityaspnet.asp"&gt;Template merging with NVelocity and ASP.NET&lt;/a&gt;&lt;/p&gt;&lt;img src="http://dotnetslackers.com/Community/aggbug.aspx?PostID=138" width="1" height="1"&gt;</description><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://dotnetslackers.com/Community/blogs/simoneb/archive/tags/Tools/default.aspx">Tools</category></item></channel></rss>