<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/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>Charlie Calvert's Community Blog : LINQ</title><link>http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx</link><description>Tags: LINQ</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Tech Republic Posts a Review of Essential LINQ</title><link>http://blogs.msdn.com/charlie/archive/2009/08/28/tech-republic-posts-a-review-of-essential-linq.aspx</link><pubDate>Fri, 28 Aug 2009 23:46:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9888802</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/charlie/comments/9888802.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=9888802</wfw:commentRss><description>&lt;p&gt;On the &lt;a href="http://blogs.techrepublic.com.com/programming-and-development/?p=1609"&gt;Tech Republic&lt;/a&gt; web site &lt;a href="http://search.techrepublic.com.com/search/Justin+James.html"&gt;Justin James&lt;/a&gt; has &lt;a href="http://blogs.techrepublic.com.com/programming-and-development/?p=1609"&gt;written a review&lt;/a&gt; of &lt;a href="http://www.amazon.com/Essential-LINQ-Charlie-Calvert/dp/0321564162/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1251491498&amp;amp;sr=8-1"&gt;Essential LINQ&lt;/a&gt;, the book Dinesh Kulkarni and I wrote about the key features of LINQ such as &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;LINQ to Objects &lt;/li&gt;    &lt;li&gt;LINQ to SQL &lt;/li&gt;    &lt;li&gt;LINQ to XML &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We also touch on important subjects such as the Entity Framework, best practices, and LINQ extensions.&lt;/p&gt;  &lt;p&gt;This isn’t the place to rehash the review, but I’ll admit that I found reason to enjoy it. For instance, Justin says: “Overall, I was absolutely delighted with the extraordinarily well written&lt;em&gt; Essential LINQ&lt;/em&gt;. It’s one of the few technical books I’ve read that didn’t feel like a chore to read.”&lt;/p&gt;  &lt;p&gt;The key links:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.techrepublic.com.com/programming-and-development/?p=1609"&gt;Read the Review&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Tech Republic also provides a link to a &lt;a href="http://downloads.techrepublic.com.com/abstract.aspx?docid=1121575"&gt;sample chapter&lt;/a&gt; from the book. &lt;/li&gt; &lt;/ul&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f08%2f28%2ftech-republic-posts-a-review-of-essential-linq.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f08%2f28%2ftech-republic-posts-a-review-of-essential-linq.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9888802" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>Recent Videos from the C# Team (2008-2009)</title><link>http://blogs.msdn.com/charlie/archive/2009/07/31/recent-videos-from-the-c-team.aspx</link><pubDate>Fri, 31 Jul 2009 23:17:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9854671</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/charlie/comments/9854671.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=9854671</wfw:commentRss><description>&lt;p&gt;Here is a selection of videos created between July, 2008 and July, 2009 by members of the C# team at Microsoft:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://channel9.msdn.com/posts/Charles/C-40-Meet-the-Design-Team/"&gt;C# 4.0 Meet the Design Team&lt;/a&gt;: &lt;strong&gt;Anders Hejlsberg, Eric Lippert&lt;/strong&gt;, et al &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2008/07/25/video-new-error-checking-feature-in-visual-studio-service-pack-1.aspx"&gt;Error Checking Feature in Visual Studio Service Pack 1&lt;/a&gt;: &lt;strong&gt;DJ Park&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friendly/"&gt;Inside C# 4.0 - Dynamic Typing, Optional Params, Variance&lt;/a&gt;:&lt;strong&gt; Sam Ng&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://channel9.msdn.com/posts/Charles/Anders-Hejlsberg-and-Guy-Steele-Concurrency-and-Language-Design/"&gt;Concurrency and Language Design&lt;/a&gt;:&lt;strong&gt; Anders Hejlsberg&lt;/strong&gt; and&lt;strong&gt; Guy Steele&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://channel9.msdn.com/pdc2008/TL16/"&gt;The Future of C#&lt;/a&gt;: &lt;strong&gt;Anders Hejlsberg&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/emea/teched2008/developer/tv/"&gt;The Joy of Writing Code in Visual Studio 2010&lt;/a&gt;: &lt;strong&gt;Karen Liu&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2008/11/13/anders-hejlsberg-video-on-c-dynamic.aspx"&gt;C# 4.0 Video on Dynamic&lt;/a&gt;: &lt;strong&gt;Anders Hejlsberg&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2008/12/03/jeff-richter-video-on-asynchronous-programming-and-his-power-threading-library.aspx"&gt;Asynchronous Programming Power Threading Library&lt;/a&gt;: &lt;strong&gt;Jeff Richter &lt;/strong&gt;and &lt;strong&gt;Charlie Calvert&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Anders-Hejlsberg-The-Future-of-C/"&gt;Expert to Expert – The Future of C#&lt;/a&gt;: &lt;strong&gt;Erik Meijer &lt;/strong&gt;and &lt;strong&gt;Anders Hejlsberg&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://channel9.msdn.com/posts/Charles/Emmanuel-Stapf-Eiffel-and-Contract-Oriented-Programming/"&gt;Eiffel and Contract Oriented Programming&lt;/a&gt;: &lt;strong&gt;Mads Torgersen&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://channel9.msdn.com/posts/Charles/Eric-Lippert-Happy-Birthday-C9/"&gt;Eric Lippert Happy Birthday&lt;/a&gt;: &lt;strong&gt;Eric Lippert&lt;/strong&gt; &lt;/li&gt; &lt;/ul&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f07%2f31%2frecent-videos-from-the-c-team.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f07%2f31%2frecent-videos-from-the-c-team.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9854671" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/charlie/archive/tags/Video/default.aspx">Video</category><category domain="http://blogs.msdn.com/charlie/archive/tags/Dynamic/default.aspx">Dynamic</category><category domain="http://blogs.msdn.com/charlie/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category><category domain="http://blogs.msdn.com/charlie/archive/tags/Parallel/default.aspx">Parallel</category></item><item><title>Community Convergence XLVIII</title><link>http://blogs.msdn.com/charlie/archive/2009/01/13/community-convergence-xlviii.aspx</link><pubDate>Wed, 14 Jan 2009 08:03:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9318012</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/charlie/comments/9318012.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=9318012</wfw:commentRss><description>&lt;p&gt;Welcome to the 48th Community Convergence. The C# team continues to work hard to get out the next version of C#, and to add improvements to the &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=922B4655-93D0-4476-BDA4-94CF5F8D4814&amp;amp;displaylang=en"&gt;Visual Studio 2010&lt;/a&gt; IDE. Working long, fruitful hours on these rainy Washington State January days and nights, the engineers on our team are doing a great job putting together a set of features that will improve both C#, and the experience C# developers have in the Visual Studio IDE. They are also working to lay the foundation for some big improvements which you will see only after the next version of C# ships.&lt;/p&gt; &lt;p&gt;To keep up with the team's plans, you should:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Continue to follow LINQ  &lt;li&gt;Learn about the Dynamic Features planned for Visual Studio 2010  &lt;li&gt;Learn about the Code Focused IDE features planned for Visual Studio 2010&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Here are a few listings that will help you gain deeper insight into all of these important technology:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Videos&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://www.informit.com/podcasts/episode.aspx?e=ff719d1a-67c8-47c9-86f4-0d31f2723e6f"&gt;Anders Hejlsberg, Mads Torgersen and Eric Lippert discuss the new Features in C# 4.0, Part I&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://www.informit.com/podcasts/episode.aspx?e=4b0da588-4e09-4f4a-bb8a-2c1d7d2ff1f6"&gt;Anders Hejlsberg, Mads Torgersen and Eric Lippert discuss the new Features in C# 4.0, Part II&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Sam Ng&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/samng/archive/2008/12/24/dynamic-in-c-vii-phantom-method-semantics.aspx"&gt;Dynamic in C# VII: Phantom Method Semantics&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/samng/archive/2008/12/15/dynamic-in-c-vi-what-dynamic-does-not-do.aspx"&gt;Dynamic in C# VI: What Dynamic Does not Do&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Luca Bolognese&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/lucabol/archive/2008/11/20/expression-tree-serialization-code-posted-on-code-gallery.aspx"&gt;Expression Tree Serialization and LINQ&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Eric Lippert&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/01/12/book-news.aspx"&gt;Eric has put together a short, but helpful description of how his fellow team members wrote about the C# Spec in the new book, &lt;strong&gt;The C# Programming Language Language&lt;/strong&gt;, Third Edition.&lt;/a&gt; The authors are Anders Hejlsberg, Scott Wiltamuth, Peter Golde, and Mads Torgersen. Other notable luminaries contributed annotations to the printed version of this text. All the authors are very talented and work hard, but In my opinion, Mads deserves a special note of thanks for the great work he did to help pull this edition of the book together.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Kirill Osenkov&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/archive/2009/01/10/call-hierarchy-navigation-in-visual-studio-2010.aspx"&gt;Call Hierarchy Navigation in Visual Studio 2010.&lt;/a&gt;  &lt;li&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/archive/2008/12/31/reflections-on-coding-style.aspx"&gt;Reflections on Coding Style&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Soma&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/somasegar/archive/2008/12/19/code-focused-development-in-vs-2010.aspx"&gt;New Features in Visual Studio 2010&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Matt Warren&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blogs.msdn.com/mattwar/archive/2008/11/21/linq-iqtoolkit-on-codeplex.aspx"&gt;LINQ IQ Toolkit on Codeplex&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f01%2f13%2fcommunity-convergence-xlviii.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2009%2f01%2f13%2fcommunity-convergence-xlviii.aspx"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9318012" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/Community+Convergence/default.aspx">Community Convergence</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/charlie/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://blogs.msdn.com/charlie/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category></item><item><title>LINQ Reference Documentation</title><link>http://blogs.msdn.com/charlie/archive/2008/11/23/linq-reference-documentation.aspx</link><pubDate>Mon, 24 Nov 2008 09:33:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9136045</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/charlie/comments/9136045.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=9136045</wfw:commentRss><description>&lt;p&gt;The LINQ documentation created by Microsoft is available both inside Visual Studio, and for free via the MSDN library found on the WEB. Here are some important pages from that documentation that can help you navigate through the online reference material that Microsoft has prepared for LINQ developers:  &lt;p&gt;· The Root MSDN Library Page: &lt;a href="http://msdn.microsoft.com/en-us/library/default.aspx"&gt;http://msdn.microsoft.com/en-us/library/default.aspx&lt;/a&gt;  &lt;p&gt;· .NET Development: &lt;a href="http://msdn.microsoft.com/en-us/library/aa139615.aspx"&gt;http://msdn.microsoft.com/en-us/library/aa139615.aspx&lt;/a&gt;  &lt;p&gt;· .NET Framework 3.5: &lt;a href="http://msdn.microsoft.com/en-us/library/w0x726c2.aspx"&gt;http://msdn.microsoft.com/en-us/library/w0x726c2.aspx&lt;/a&gt;  &lt;p&gt;· System.Linq: &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.aspx"&gt;http://msdn.microsoft.com/en-us/library/system.linq.aspx&lt;/a&gt;  &lt;p&gt;· System.Data.Linq: &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.linq.aspx"&gt;http://msdn.microsoft.com/en-us/library/system.data.linq.aspx&lt;/a&gt;  &lt;p&gt;· System.Xml.Linq: &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.linq.aspx"&gt;http://msdn.microsoft.com/en-us/library/system.xml.linq.aspx&lt;/a&gt;  &lt;p&gt;· Code Generation: &lt;a href="http://msdn.microsoft.com/en-us/library/bb399400.aspx"&gt;http://msdn.microsoft.com/en-us/library/bb399400.aspx&lt;/a&gt; &lt;p&gt;· More on Joins: &lt;a href="http://msdn.microsoft.com/en-us/library/bb311040.aspx"&gt;http://msdn.microsoft.com/en-us/library/bb311040.aspx&lt;/a&gt; &lt;p&gt;· For more information on the operators, see the section of the online help called “The .NET Standard Query Operators.” It is written by Anders Hejlsberg and Mads Torgersen. The URL is &lt;a href="http://msdn.microsoft.com/en-us/library/bb394939.aspx"&gt;http://msdn.microsoft.com/en-us/library/bb394939.aspx&lt;/a&gt; &lt;p&gt;I would not suggest using these references materials as a primary means of learning LINQ. However, if you have a book or other guide to LINQ development, then this reference material can be a useful addendum to that text. If you understand in a general way how LINQ works, but need answers to detailed questions, the links provided here may sometimes help you find answers.&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f11%2f23%2flinq-reference-documentation.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f11%2f23%2flinq-reference-documentation.aspx"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9136045" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx</link><pubDate>Tue, 28 Oct 2008 21:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9017751</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>35</slash:comments><comments>http://blogs.msdn.com/charlie/comments/9017751.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=9017751</wfw:commentRss><description>&lt;p&gt;This post covers the upcoming C# 4.0 support for covariance and contravariance when working with delegates and interfaces. Eric Lippert’s &lt;a href="http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx"&gt;series of posts&lt;/a&gt; on this subject are definitely the definitive reference at this time. I’m writing this overview of the subject simply as an appendix to his explanation, and as quick reference for folks who want to get up to speed on this technology. Please remember that this post covers pre-beta technology as defined in the October 2008 CTP of Visual Studio 2010.&lt;/p&gt;  &lt;p&gt;The support for covariance and contravariance in the next version of the C# language will ensure that delegates and interfaces will behave as expected when you are working with generic types. In Visual Studio 2008, there are rare occasions when developers might expect a delegate or interface to behave one way, only to find that it does not conform to their expectations. In Visual Studio 2010 delegates will behave as expected. Other C# types support have always automatically supported covariance and contravariance. C# 4.0 will simply ensure that generic delegates and interfaces follow suit.&lt;/p&gt;  &lt;p&gt;Consider this simple class hierarchy:&lt;/p&gt;  &lt;p&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Animal&lt;/span&gt;{ }     &lt;br /&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Cat&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Animal&lt;/span&gt;{ }     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;Here we have a class called &lt;strong&gt;Animal&lt;/strong&gt;, and a simple descendant of it called &lt;strong&gt;Cat&lt;/strong&gt;. Suppose you declare a delegate declaration that defines a method that returns an arbitrary type:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;delegate &lt;/span&gt;T &lt;span style="color: #2b91af"&gt;Func1&lt;/span&gt;&amp;lt;T&amp;gt;();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;&lt;font color="#000000"&gt;An instance of this delegate could be defined to return a Cat:&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Func1&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Cat&lt;/span&gt;&amp;gt; cat = () =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Cat&lt;/span&gt;();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The delegate declaration shown here defines a delegate that returns a type T. The second line of code initializes T to be of type &lt;strong&gt;Cat&lt;/strong&gt; and assigns this delegate to a simple lambda that creates an instance of a cat and returns it. For those of you not yet comfortable with lambda syntax, I’ll add that the lambda shown here is simply a shorthand way of writing a method that looks like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Cat &lt;/span&gt;MyFunc()
{
    &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Cat&lt;/span&gt;();
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;So one could also have written the cat delegate like this:&lt;/p&gt;

&lt;p&gt;&lt;span style="color: #2b91af"&gt;Func1&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Cat&lt;/span&gt;&amp;gt; cat = MyFunc;&lt;/p&gt;

&lt;p&gt;Given either of these declarations, our intuition tells us that we could assign a &lt;strong&gt;cat&lt;/strong&gt; to a delegate that returns an &lt;strong&gt;Animal&lt;/strong&gt;:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Func1&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Animal&lt;/span&gt;&amp;gt; animal = cat;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This code looks like it should succeed because a &lt;strong&gt;Cat&lt;/strong&gt; is type of &lt;strong&gt;Animal&lt;/strong&gt;, and one should always be able to assign a smaller type of object, such as a &lt;strong&gt;Cat&lt;/strong&gt;, to a larger type of object, such as a &lt;strong&gt;Animal&lt;/strong&gt;.&amp;#160; We think of the type &lt;strong&gt;Animal&lt;/strong&gt; as being large, since a creature such as a &lt;strong&gt;Cat&lt;/strong&gt;, &lt;strong&gt;Dog&lt;/strong&gt;, &lt;strong&gt;Whale&lt;/strong&gt; or &lt;strong&gt;Bird&lt;/strong&gt; would be a type of &lt;strong&gt;Animal&lt;/strong&gt;, and hence compatible with it. In other words, a big type like an &lt;strong&gt;Animal&lt;/strong&gt; is assignment compatible with lots of smaller types, such as a &lt;strong&gt;Cat&lt;/strong&gt;, &lt;strong&gt;Dog&lt;/strong&gt; or &lt;strong&gt;Whale&lt;/strong&gt;. We think of the type &lt;strong&gt;Cat&lt;/strong&gt; as being smaller than an &lt;strong&gt;Animal&lt;/strong&gt;, since only other cats can be assigned to it. It is not assignment compatible with as wide a range of types as an &lt;strong&gt;Animal&lt;/strong&gt;, and hence we think of it as being smaller.&lt;/p&gt;

&lt;p&gt;In fact, the assignment shown above does not work in Visual Studio 2008. The new feature being added in Visual Studio 2010 ensures that this assignment will work &lt;em&gt;if you make one minor change to the declaration for your delegate&lt;/em&gt;. In particular, you need to use the keyword &lt;strong&gt;out&lt;/strong&gt; in your type parameter:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;delegate &lt;/span&gt;T &lt;span style="color: #2b91af"&gt;Func1&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;out &lt;/span&gt;T&amp;gt;();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now the assignment will succeed, and the code will run as expected, as shown in Listing 1. This type of assignment is called covariance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Listing 1: Examples of covariance and contravariance.&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Linq;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Text;

&lt;span style="color: blue"&gt;namespace &lt;/span&gt;SimpleVariance
{
    &lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Animal &lt;/span&gt;{ }
    &lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Cat&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Animal &lt;/span&gt;{ }

    &lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Program
    &lt;/span&gt;{
        &lt;span style="color: green"&gt;// To understand what the new CoVariance and ContraVariance code does for you
        // Try deleting or adding the words out and in from the following 2 lines of code:
        &lt;/span&gt;&lt;span style="color: blue"&gt;delegate &lt;/span&gt;T &lt;span style="color: #2b91af"&gt;Func1&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;out &lt;/span&gt;T&amp;gt;();
        &lt;span style="color: blue"&gt;delegate void &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action1&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;in &lt;/span&gt;T&amp;gt;(T a);

        &lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
        {
           &lt;span style="color: green"&gt;// Covariance
            &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func1&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Cat&lt;/span&gt;&amp;gt; cat = () =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Cat&lt;/span&gt;();
            &lt;span style="color: #2b91af"&gt;Func1&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Animal&lt;/span&gt;&amp;gt; animal = cat;

            &lt;span style="color: green"&gt;// Contravariance
            &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action1&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Animal&lt;/span&gt;&amp;gt; act1 = (ani) =&amp;gt; { &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(ani); };
            &lt;span style="color: #2b91af"&gt;Action1&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Cat&lt;/span&gt;&amp;gt; cat1 = act1;
        }        
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Looking at Listing 1, you will notices that there is a second example included in it. This second example illustrates contravariance. Most developers, including myself, find contravariance more difficult to understand than covariance. It is, however, a similar concept.&lt;/p&gt;

&lt;p&gt;The second example starts out by including a delegate declaration. This time the declaration defines a method that takes a parameter, rather than returning a value:&lt;/p&gt;

&lt;p&gt;&lt;span style="color: blue"&gt;delegate void &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action1&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;in &lt;/span&gt;T&amp;gt;(T a); &lt;/p&gt;

&lt;p&gt;Notice also that we use the keyword &lt;strong&gt;in&lt;/strong&gt;, rather than the keyword &lt;strong&gt;out&lt;/strong&gt;. That is because we are passing a parameter &lt;strong&gt;in&lt;/strong&gt;, rather than returning a result “back &lt;strong&gt;out&lt;/strong&gt;” of a function.&lt;/p&gt;

&lt;p&gt;Next we define a delegate that takes an animal as a parameter:&lt;/p&gt;

&lt;p&gt;&lt;span style="color: #2b91af"&gt;Action1&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Animal&lt;/span&gt;&amp;gt; act1 = (ani) =&amp;gt; { &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(ani); };&lt;/p&gt;

&lt;p&gt;In the contravariant example shown in Listing 1, the assignment of &lt;strong&gt;act1&lt;/strong&gt; to the delegate &lt;strong&gt;cat1&lt;/strong&gt; would fail if we did not include the keyword in when declaring the Action1 delegate type. Here is the assignment:&lt;/p&gt;

&lt;p&gt;&lt;span style="color: #2b91af"&gt;Action1&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Cat&lt;/span&gt;&amp;gt; cat1 = act1; &lt;/p&gt;

&lt;p&gt;This would fail in C# 3.0 no matter what you did. It will work in C# 4.0 so long as you use the keyword &lt;strong&gt;in&lt;/strong&gt; when declaring the delegate type &lt;strong&gt;Action1&lt;/strong&gt;. If you omitted the keyword, then it would fail. For instance, the following declaration of &lt;strong&gt;Action1&lt;/strong&gt; would cause the assigned to fail because the keyword &lt;strong&gt;in&lt;/strong&gt; is omitted:&lt;/p&gt;

&lt;p&gt;&lt;span style="color: blue"&gt;delegate void &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action1&lt;/span&gt;&amp;lt;T&amp;gt;(T a); &lt;/p&gt;

&lt;p&gt;Contrast this declaration with the one in Listing 1.&lt;/p&gt;

&lt;p&gt;In the contravariant example we use a lambda to define a method that is equivalent to a standard method that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;font color="#0000ff"&gt;public static void&lt;/font&gt; Contra(&lt;font color="#2b91af"&gt;Animal&lt;/font&gt; ani) 

  &lt;br /&gt;{ 

  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(ani); 

  &lt;br /&gt;}&lt;/p&gt;

&lt;p&gt;I’m using lambdas in Listing 1 simply to keep the code short, not because lambdas are connected to covariance and contravariance. This code would still illustrate covariance and contravariance whether or not I used lambdas. What is important here is not the presence of lambdas, but the presence of a generic delegate. &lt;/p&gt;

&lt;p&gt;Finally, lets take one moment to make sure you understand why the assignment of &lt;strong&gt;act1&lt;/strong&gt; to &lt;strong&gt;cat1&lt;/strong&gt; works. The peculiar thing about contravariance is that it appears that we are assigning a larger type to a smaller type. In other words, it appears that we are trying to fit a big thing like an &lt;strong&gt;Animal&lt;/strong&gt;, into a small type like a &lt;strong&gt;Cat&lt;/strong&gt;. This is not what we are doing. The point here is that &lt;strong&gt;act1&lt;/strong&gt; will work with any animal, and a &lt;strong&gt;Cat&lt;/strong&gt; is animal, therefore you can safely make the assignment. In other words, &lt;strong&gt;cat1&lt;/strong&gt; can only be passed &lt;strong&gt;Cats&lt;/strong&gt;, and all cats are animals, therefore anything you can assign to &lt;strong&gt;cat1&lt;/strong&gt; could also be safely passed to &lt;strong&gt;act1&lt;/strong&gt;. This is why the assignment should work, and does in fact work if you use the keyword &lt;strong&gt;in&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;As I say, contravariance is a bit confusing. If you find the subject a bit much, then you are joining a happy throng of other developers who struggle with the subject. My suggestion is just to relax, think about the example I’ve shown here for a bit, and you will probably have an “ah-ha” moment either in the next few moments, or sometime in the hopefully very lengthy remainder of your life.&lt;/p&gt;

&lt;p&gt;Almost everyone who has written or talked about this subject, from Eric Lippert to Anders himself, points out that this technology is not so much an innovation as a means of bringing C# in line with developers expectations. Adding support for covariance and contravariance to generic interfaces and delegates simply ensures that the language behaves as many would intuitively expect it to behave. All you have to do is remember to add the the keywords &lt;strong&gt;in&lt;/strong&gt; or &lt;strong&gt;out&lt;/strong&gt; when you are making an assignment that involves delegates or interfaces, and the language isn’t behaving as you would expect it to behave. &lt;/p&gt;

&lt;p&gt;I apologize for focusing only on delegates in this post. Hopefully I’ll find the time to come back and illustrate the same subject using interfaces. I should add that all the code shown here is written against a very shaky pre-beta version of the C# 4.0, and it is always possible, though that not necessarily likely, that this technology will change before it ships.&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f10%2f27%2flinq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f10%2f27%2flinq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9017751" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LinqFarm/default.aspx">LinqFarm</category></item><item><title>Community Convergence XLVI</title><link>http://blogs.msdn.com/charlie/archive/2008/10/25/community-convergence-ixv.aspx</link><pubDate>Sun, 26 Oct 2008 09:40:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9016528</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/charlie/comments/9016528.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=9016528</wfw:commentRss><description>&lt;p&gt;Welcome to the forty-sixth Community Convergence. There are three upcoming events for members of the C# team. The most important, &lt;a href="http://www.microsoftpdc.com/Default.aspx"&gt;PDC&lt;/a&gt;, will be held in Los Angeles over the week of October 26. The weekend before the conference, there will be a &lt;a href="http://www.socalcodecamp.com/"&gt;Code Camp in Los Angeles&lt;/a&gt;. One week after the PDC, there will be a &lt;a href="http://www.microsoft.com/emea/teched2008/developer/default.aspx"&gt;Tech Ed&lt;/a&gt; in Spain.&lt;/p&gt;  &lt;h3&gt;Code Camp in Los Angeles&lt;/h3&gt;  &lt;p&gt;Members of the team will be at the &lt;a href="http://www.socalcodecamp.com/"&gt;Code Camp in Los Angeles&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Talk: Essence of LINQ      &lt;ul&gt;       &lt;li&gt;Speaker: Charlie Calvert &lt;/li&gt;        &lt;li&gt;Room: GFS 116 &lt;/li&gt;        &lt;li&gt;Time 9 AM &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Talk: C# 3.0 features and how to (ab)use them      &lt;ul&gt;       &lt;li&gt;Speaker: Sam Ng &lt;/li&gt;        &lt;li&gt;GFS 116 &lt;/li&gt;        &lt;li&gt;Time: 10:15 AM &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;How LINQ Words      &lt;ul&gt;       &lt;li&gt;Speaker: Alex Tuner &lt;/li&gt;        &lt;li&gt;Room: SLH 102 &lt;/li&gt;        &lt;li&gt;Time: 12:30 PM &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;PDC&lt;/h3&gt;  &lt;p&gt;The following list includes talks by members of the C#, VB, F#, IronPython and IronRuby teams that will be given at &lt;a href="http://www.microsoftpdc.com/Default.aspx"&gt;PDC&lt;/a&gt;.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;TL16: The Future of C#      &lt;ul&gt;       &lt;li&gt;Anders Hejlsberg &lt;/li&gt;        &lt;li&gt;Mon 10/27 | 1:45 PM-3:00 PM | Petree Hall CD &lt;/li&gt;        &lt;li&gt;Repeated: Wed 10/29 | 3:00 PM-4:15 PM | 502A &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;TL10: Deep Dive: Dynamic Languages in .NET      &lt;ul&gt;       &lt;li&gt;Speaker: Jim Hugunin &lt;/li&gt;        &lt;li&gt;Mon 10/27 | 3:30PM | 502A &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;TL11: An Introduction to Microsoft F#      &lt;ul&gt;       &lt;li&gt;Speaker: Luca Bolognese &lt;/li&gt;        &lt;li&gt;Thu 10/30 | 1:45 PM-3:00 PM | 502A &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;TL12: Future Directions for Microsoft Visual Basic      &lt;ul&gt;       &lt;li&gt;Speaker: Paul Vick &lt;/li&gt;        &lt;li&gt;Tue 10/28 | 5:15 PM-6:30 PM | 406A &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;TL46: Microsoft Visual C# IDE: Tips and Tricks      &lt;ul&gt;       &lt;li&gt;Dustin Campbell &lt;/li&gt;        &lt;li&gt;Mon 10/27 | 12:45-13:30 | 403AB &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;TL54: Natural Interop with Silverlight, Office and Python      &lt;ul&gt;       &lt;li&gt;Alex Turner &lt;/li&gt;        &lt;li&gt;Tue 10/28 | 12:45 PM-1:30 PM | 404A &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;TL57: A Panel on the Future of Programming Languages      &lt;ul&gt;       &lt;li&gt;G. Bracha, D.Crockford, A. Hejlsberg, E. Meijer, W.Schulte, J. Siek &lt;/li&gt;        &lt;li&gt;Wed 10/29 | 10:30 AM-11:45 AM | 403AB &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;TL44 IronRuby: The Right Language for the Right Job      &lt;ul&gt;       &lt;li&gt;Speaker: John Lam &lt;/li&gt;        &lt;li&gt;Mon 10/27, 5:15PM, 515B &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Tech Ed Europe (EMEA)&lt;/h3&gt;  &lt;p&gt;Here are some talks by the C# and VB teams that will be given at &lt;a href="http://www.microsoft.com/emea/teched2008/"&gt;Tech Ed EMEA&lt;/a&gt;.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;How LINQ Works in C#      &lt;ul&gt;       &lt;li&gt;Speaker: Alex Turner &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;New Features in C# 3.0      &lt;ul&gt;       &lt;li&gt;Speaker: Mads Torgersen &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Visual C# IDE Tips and Tricks      &lt;ul&gt;       &lt;li&gt;Speaker: Karen Liu &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;The Future of C#      &lt;ul&gt;       &lt;li&gt;Speaker: Mads Torgersen &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;The Joy of Writing Code in Visual Studio 2010      &lt;ul&gt;       &lt;li&gt;Speaker: Karen Liu &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Talk: Future Directions for Visual Basic      &lt;ul&gt;       &lt;li&gt;Speaker: Jonathan Aneja &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Talk: Strategies for Upgrading Visual Basic 6.0 to .NET      &lt;ul&gt;       &lt;li&gt;Speaker: Paul Yuknewicz, Jonathan Aneja &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Microsoft Visual Basic 2008 Tips and Tricks      &lt;ul&gt;       &lt;li&gt;Speaker: Lisa Feigenbaum &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;How LINQ Works in VB      &lt;ul&gt;       &lt;li&gt;Speaker: Jonathan Aneja &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Conquering XML with Visual Basic 9.0      &lt;ul&gt;       &lt;li&gt;Speaker: Lisa Feigenbaum &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f10%2f25%2fcommunity-convergence-ixv.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f10%2f25%2fcommunity-convergence-ixv.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9016528" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/Community+Convergence/default.aspx">Community Convergence</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/charlie/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>LINQ Farm: Preserving Formatting with LINQ to XML</title><link>http://blogs.msdn.com/charlie/archive/2008/09/29/linq-farm-preserving-formats-with-linq-to-xml.aspx</link><pubDate>Tue, 30 Sep 2008 09:02:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8969897</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8969897.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8969897</wfw:commentRss><description>&lt;p&gt;In a &lt;a href="http://blogs.msdn.com/charlie/archive/2008/09/26/linq-farm-linq-to-xml-and-line-numbers.aspx"&gt;previous post&lt;/a&gt;, you saw how to work with line numbers when using LINQ to XML to read a file. This post continues in the same vein, but this time the focus is on how to:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Read in an XML file with an arbitrary format, and then write it back out to disk in exactly the same format. &lt;/li&gt;    &lt;li&gt;Read in an XML file with an arbitrary format, and write it back out with standard formatting &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Some scenarios where you might need this functionality include reading a document from one location and writing it to another location, or reading in a document, editing it, and writing it back out with new data, but the same format.&lt;/p&gt;  &lt;p&gt;You can download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=3280"&gt;source&lt;/a&gt; for the code shown in this post.&lt;/p&gt;  &lt;h2&gt;How to Preserve Formatting&lt;/h2&gt;  &lt;p&gt;Consider the following block of XML:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;xml&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;version&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;1.0&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;encoding&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt; 
&lt;/span&gt; sam 
&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;This document has an extremely idiosyncratic layout. Tags are nested on the same line and a text field stands on its own surrounded by linefeeds. If you wish to read in and write this document back out with the formatting preserved, here is how to proceed:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;XDocument&lt;/span&gt; x = &lt;span style="color: rgb(43,145,175)"&gt;XDocument&lt;/span&gt;.Load(readFileName, &lt;span style="color: rgb(43,145,175)"&gt;LoadOptions&lt;/span&gt;.PreserveWhitespace);
x.Save(writeFileName, &lt;span style="color: rgb(43,145,175)"&gt;SaveOptions&lt;/span&gt;.DisableFormatting);&lt;/pre&gt;

&lt;p&gt;When calling &lt;strong&gt;XDocument.Load&lt;/strong&gt;, pass in the &lt;strong&gt;LoadOptions.PreserveWhitespace&lt;/strong&gt; flag from the following enumeration: &lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;enum&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;LoadOptions
&lt;/span&gt;{
  None = 0,
  PreserveWhitespace = 1,
  SetBaseUri = 2,
  SetLineInfo = 4
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;When writing the document back out to disk, use &lt;strong&gt;SaveOptions.DisableFormatting&lt;/strong&gt;, from the following enumeration:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;enum&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;SaveOptions
&lt;/span&gt;{
  None = 0,
  DisableFormatting = 1
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;If you take these two steps, then the XML you write to disk will have the same formatting as the document you read in. &lt;/p&gt;

&lt;h2&gt;Modifying Oddly Formatted XML&lt;/h2&gt;

&lt;p&gt;If you read in XML that has an odd format, it is likely that linefeeds play a role in that formatting. Suppose we want to modify the XML document shown previously so that it looks like this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;xml&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;version&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;1.0&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;encoding&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
&lt;/span&gt;  Sue  
&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The value &lt;strong&gt;sam&lt;/strong&gt; from the original document has been replaced with the word &lt;strong&gt;Sue&lt;/strong&gt;. To make this substitution properly, you need to take care to preserve the linefeeds in the original document. In this example I show a relatively mindless way to preserve linefeeds. For good measure, I also add in code for working with line numbers.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;XDocument&lt;/span&gt; x2 = &lt;span style="color: rgb(43,145,175)"&gt;XDocument&lt;/span&gt;.Load(readFileName,
    &lt;span style="color: rgb(43,145,175)"&gt;LoadOptions&lt;/span&gt;.PreserveWhitespace | &lt;span style="color: rgb(43,145,175)"&gt;LoadOptions&lt;/span&gt;.SetLineInfo);

&lt;span style="color: rgb(43,145,175)"&gt;XText&lt;/span&gt; value = (&lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; c &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; x2.Elements().DescendantNodes().OfType&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;XText&lt;/span&gt;&amp;gt;()
               &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; c.Value.Trim().Length &amp;gt; 0
               &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; c).Single();

&lt;span style="color: rgb(43,145,175)"&gt;IXmlLineInfo&lt;/span&gt; x = (&lt;span style="color: rgb(43,145,175)"&gt;IXmlLineInfo&lt;/span&gt;)value;
&lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;&amp;quot;Line Number: {0}&amp;quot;&lt;/span&gt;, x.LineNumber);
value.Value = &lt;span style="color: rgb(43,145,175)"&gt;Environment&lt;/span&gt;.NewLine + &lt;span style="color: rgb(163,21,21)"&gt;&amp;quot;  Sue  &amp;quot;&lt;/span&gt; + &lt;span style="color: rgb(43,145,175)"&gt;Environment&lt;/span&gt;.NewLine;

x2.Save(createFileName, &lt;span style="color: rgb(43,145,175)"&gt;SaveOptions&lt;/span&gt;.DisableFormatting);&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;This code reads in the original XML file with the &lt;strong&gt;PreserverWhitespace&lt;/strong&gt; flag. I've also OR'd in the &lt;strong&gt;LoadOptions.SetLineInfo&lt;/strong&gt; flag. I do this not because I have any real need to do so, but simply so you can see how to pass in two flags to the &lt;strong&gt;Load&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;The next block of code queries the document for all the text nodes that consist of something more than pure white space. I then output the line number of the node that was found. Finally, I modify the node, replacing the original text with the word &lt;strong&gt;Sue&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;value.Value = &lt;span style="color: rgb(43,145,175)"&gt;Environment&lt;/span&gt;.NewLine + &lt;span style="color: rgb(163,21,21)"&gt;&amp;quot; Sue &amp;quot;&lt;/span&gt; + &lt;span style="color: rgb(43,145,175)"&gt;Environment&lt;/span&gt;.NewLine; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To modify the node, I simply replace the &lt;strong&gt;Value&lt;/strong&gt; property with new text. Note that the code explicitly adds in a pair of linefeeds to preserves the original, idiosyncratic formatting. If you are confused about what is happening here, look up again at the original document. It begins with two tags:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then there is a linefeed, the word &lt;strong&gt;sam&lt;/strong&gt;, and another linefeed and finally the closing tags: &lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The code I've written simply preserves those linefeeds. I'm doing this not because I think it is a good idea to include white space like this in a document, but simply to show you that it is possible to preserve it if you have a need or desire to do so. &lt;/p&gt;

&lt;p&gt;After modifying the document, the XML is written back to disk using the same technique explained in the previous section. &lt;/p&gt;

&lt;h2&gt;Cleaning Up XML&lt;/h2&gt;

&lt;p&gt;If you discover an XML file that has idiosyncratic formatting that you want to remove, LINQ to XML makes it fairly easy to clean up such a document. By default, LINQ to XML will write out a document with proper formatting. All you have to do is use the &lt;strong&gt;Load&lt;/strong&gt; and &lt;strong&gt;Save&lt;/strong&gt; methods without any of the &lt;strong&gt;LoadOptions&lt;/strong&gt; or &lt;strong&gt;SaveOption&lt;/strong&gt; flags shown in the previous two sections:&lt;/p&gt;

&lt;pre&gt;XDocument x2 = XDocument.Load(readFileName);
x2.Save(createFileName);&lt;/pre&gt;

&lt;p&gt;This code will not, however, remove arbitrary white space from a text node. For instance, the text node described above that begins and ends with a linefeed will still have linefeeds when you write the document back out. This may be exactly what you want. On the other hand, you may feel it leaves your document in a pretty ugly state. Here is how to clean up the problem&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;XDocument&lt;/span&gt; x2 = &lt;span style="color: rgb(43,145,175)"&gt;XDocument&lt;/span&gt;.Load(readFileName);

&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; query = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; c &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; x2.Elements().DescendantNodes().OfType&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;XText&lt;/span&gt;&amp;gt;()
            &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; c;

&lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; query)
{
    item.Value = item.Value.Trim();
}

x2.Save(createFileName);&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This code finds all the text nodes in the document and strips away their white space with standard string &lt;strong&gt;Trim&lt;/strong&gt; method. When you are done, you can write the document back out to disk. It will now have the clean formatting you expect: &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;xml&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;version&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;1.0&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;encoding&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;sam&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Finally, if you want to strip away all white space, and end up with a document that sits on a single line, you can write code that looks just like the listing shown earlier in this section, except that the the call to &lt;strong&gt;Save&lt;/strong&gt; the document should look like this:&lt;/p&gt;

&lt;pre&gt;x2.Save(createFileName, SaveOptions.DisableFormatting);&lt;/pre&gt;

&lt;p&gt;The output you create will appear all on one line, like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;xml&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;version&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;1.0&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;encoding&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;?&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;sam&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;If you did not clean up the text nodes as shown above, then your code would end up with linefeeds in it from the text nodes. The result, in our case, would look like this: &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;xml&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;version&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;1.0&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt; &lt;/span&gt;&lt;span style="color: rgb(255,0,0)"&gt;encoding&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color: rgb(0,0,255)"&gt;?&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
&lt;/span&gt;  sam  
&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;beta&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;alpha&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The point here is that &lt;strong&gt;DisableFormatting&lt;/strong&gt; turns off LINQ to XML’s attempts to format your code properly. If you read in strangely formatted code, then using this flag will preserve that idiosyncratic formatting. If you read in code with no attempt to preserve the original formatting, as we do in this section, then writing to disk with this option will end up stripping away all formatting, and leaving you with code all on one line except for white space in a text node.&lt;/p&gt;

&lt;h2&gt;Working with Text Files&lt;/h2&gt;

&lt;p&gt;In this post I've focused on reading in and writing XML files using the LINQ to XML API. It's perhaps useful to recall that you can also use standard .NET IO in order to perform similar tasks. This can be particularly helpful when you are testing your code, and want to be sure that your LINQ to XML routines are behaving as expected.&lt;/p&gt;

&lt;p&gt;Here is a simple routine for reading in a XML file, or any other text file:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; ReadAsText(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; fileName)
{
    &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; s = &lt;span style="color: rgb(43,145,175)"&gt;File&lt;/span&gt;.ReadAllText(fileName);
    &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(s);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And here is the C# call for writing a file to disk:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;File&lt;/span&gt;.WriteAllText&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;If you download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=3280"&gt;sample code&lt;/a&gt; associated with this post, you will see that I use these routines to confirm that my code is working correctly. 

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;In this post you've seen how to preserve white space in an XML document, and how to strip it out and return to standard formatting. You also had a chance to learn how to edit an idiosyncratically structured XML document without changing its format.&lt;/p&gt;

&lt;p&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=3280"&gt;source&lt;/a&gt;.&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f09%2f29%2flinq-farm-preserving-formats-with-linq-to-xml.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f09%2f29%2flinq-farm-preserving-formats-with-linq-to-xml.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8969897" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LinqFarm/default.aspx">LinqFarm</category></item><item><title>LINQ Farm: LINQ to XML and Line Numbers</title><link>http://blogs.msdn.com/charlie/archive/2008/09/26/linq-farm-linq-to-xml-and-line-numbers.aspx</link><pubDate>Fri, 26 Sep 2008 23:13:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8966999</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8966999.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8966999</wfw:commentRss><description>&lt;p&gt;There are times when it is useful to know the line number of a node in an XML file. This information can be a helpful to users, particularly if you want to report an error. It can also be convenient to search for a node by line number, but that can, of course, be a very risky endeavor, as documents can be modified accidentally, and their line numbers changed without notice. &lt;/p&gt;  &lt;p&gt;This post shows a few fundamentals about working with line numbers in a LINQ to XML program. The code shown in this post is taken from a project called &lt;strong&gt;XmlLineNumber&lt;/strong&gt;. You can download this program from the &lt;a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=linqfarm&amp;amp;ReleaseId=81"&gt;LINQ Farm&lt;/a&gt; on &lt;a href="http://code.msdn.microsoft.com/"&gt;Code Gallery&lt;/a&gt;.&lt;/p&gt;  &lt;h2&gt;Reporting a Line Number&lt;/h2&gt;  &lt;p&gt;Let’s begin our exploration by detailing a technique for reporting the number of a node that you have found in an XML file. To get started we need to use code from a class called &lt;strong&gt;XObject&lt;/strong&gt;. As shown in Figure 1, &lt;strong&gt;XObject&lt;/strong&gt; sits at the top of the LINQ to XML class hierarchy.&lt;/p&gt;  &lt;p&gt;&lt;a href="https://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/LINQFarmLINQtoXMLandLineNumbers_B9D6/Chapter13-XmlHierarchy_2.png"&gt;&lt;img title="Chapter13-XmlHierarchy" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="454" alt="Chapter13-XmlHierarchy" src="https://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/LINQFarmLINQtoXMLandLineNumbers_B9D6/Chapter13-XmlHierarchy_thumb.png" width="659" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Figure 1: The core objects in the LINQ to XML class hierarchy&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;XObject&lt;/strong&gt; implements an interface called &lt;strong&gt;IXmlLineInfo&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;&lt;span style="color: blue"&gt;public interface&lt;/span&gt;&lt;span style="color: #2b91af"&gt;IXmlLineInfo      &lt;br /&gt;&lt;/span&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;int &lt;/span&gt;LineNumber { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;int &lt;/span&gt;LinePosition { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;bool &lt;/span&gt;HasLineInfo();     &lt;br /&gt;}     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;The eponymous &lt;strong&gt;LineNumber&lt;/strong&gt; property of this interface is able to store the information we want. To enlist it in our service we need only call &lt;strong&gt;XDocument.Load&lt;/strong&gt; with &lt;strong&gt;LoadOptions.SetLineInfo&lt;/strong&gt;:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;XDocument &lt;/span&gt;xml = &lt;span style="color: #2b91af"&gt;XDocument&lt;/span&gt;.Load(fileName, &lt;span style="color: #2b91af"&gt;LoadOptions&lt;/span&gt;.SetLineInfo);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If you load this XML file into memory using &lt;strong&gt;SetLineInfo&lt;/strong&gt; from the &lt;strong&gt;LoadOptions&lt;/strong&gt; enumeration, then line numbers will be associated with the nodes in your document. The file we are loading is called &lt;strong&gt;FirstFourPlanets.xml. &lt;/strong&gt;It’s a sweet little file that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;span style="color: blue"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #a31515"&gt;xml&lt;/span&gt;&lt;span style="color: red"&gt;version&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;1.0&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;encoding&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;?&amp;gt; 
    &lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Planets&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Planet&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Mercury&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moons&lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt; 
    &lt;br /&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Planet&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Planet&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Venus&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moons&lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt; 
    &lt;br /&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Planet&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Planet&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Earth&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moons&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moon&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Moon&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;OrbitalPeriod &lt;/span&gt;&lt;span style="color: red"&gt;UnitsOfMeasure&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;days&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;27.321582&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;OrbitalPeriod&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moon&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moons&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Planet&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Planet&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Mars&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moons&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moon&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Phobos&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;OrbitalPeriod &lt;/span&gt;&lt;span style="color: red"&gt;UnitsOfMeasure&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;days&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;0.318&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;OrbitalPeriod&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moon&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moon&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Deimos&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;OrbitalPeriod &lt;/span&gt;&lt;span style="color: red"&gt;UnitsOfMeasure&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;days&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;1.26244&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;OrbitalPeriod&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moon&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Moons&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Planet&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Planets&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Here is code that uses the &lt;strong&gt;IXmlLineInfo&lt;/strong&gt; interface to report the line number of a node discovered through a standard LINQ to XML search:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;XText &lt;/span&gt;phobos = (&lt;span style="color: blue"&gt;from &lt;/span&gt;x &lt;span style="color: blue"&gt;in &lt;/span&gt;xml.DescendantNodes().OfType&amp;lt;&lt;span style="color: #2b91af"&gt;XText&lt;/span&gt;&amp;gt;()
                &lt;span style="color: blue"&gt;where &lt;/span&gt;x.Value == &lt;span style="color: #a31515"&gt;&amp;quot;Phobos&amp;quot;
                &lt;/span&gt;&lt;span style="color: blue"&gt;select &lt;/span&gt;x).Single();

&lt;span style="color: blue"&gt;var &lt;/span&gt;lineInfo = (IXmlLineInfo)phobos;
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;{0} appears on line {1}&amp;quot;&lt;/span&gt;, phobos, lineInfo.LineNumber);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This code looks through all the descendants of the root node for nodes of type &lt;strong&gt;XText&lt;/strong&gt; which are equal to the word &lt;strong&gt;Phobos&lt;/strong&gt;. It uses the LINQ query operator &lt;strong&gt;Single&lt;/strong&gt; to ensure that the query returns only a single node. If the query returned more than one result, the call to &lt;strong&gt;Single&lt;/strong&gt; would raise an exception, which in this case is the behavior we want. The program then casts the result as an instance of &lt;strong&gt;IXmlLineInfo&lt;/strong&gt;, and reports the line number to the user:&lt;/p&gt;

&lt;pre&gt;Phobos appears on line 24&lt;/pre&gt;

&lt;h2&gt;Searching by Line Number&lt;/h2&gt;

&lt;p&gt;Let's now turn things around and show how to search through an XML file and look for a node by line number. If you glance at the &lt;strong&gt;FirstFourPlanets.xml&lt;/strong&gt; file, you will see that line 21 looks like this:&lt;/p&gt;

&lt;pre&gt;&amp;lt;Name&amp;gt;Mars&amp;lt;/Name&amp;gt;&lt;/pre&gt;

&lt;p&gt;Here is code from the XmlLineNumbers sample showing how to search for that node by line number:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;XDocument &lt;/span&gt;xml = &lt;span style="color: #2b91af"&gt;XDocument&lt;/span&gt;.Load(fileName, &lt;span style="color: #2b91af"&gt;LoadOptions&lt;/span&gt;.SetLineInfo);

&lt;span style="color: blue"&gt;var &lt;/span&gt;line = &lt;span style="color: blue"&gt;from &lt;/span&gt;x &lt;span style="color: blue"&gt;in &lt;/span&gt;xml.Descendants()
           &lt;span style="color: blue"&gt;let &lt;/span&gt;lineInfo = (IXmlLineInfo)x
           &lt;span style="color: blue"&gt;where &lt;/span&gt;lineInfo.LineNumber == 21
           &lt;span style="color: blue"&gt;select &lt;/span&gt;x;

&lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;item &lt;span style="color: blue"&gt;in &lt;/span&gt;line)
{
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(item);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Note that the first line uses &lt;strong&gt;LoadOptions.SetLineInfo&lt;/strong&gt; to ensures that line information is recorded when the document is loaded into memory. &lt;/p&gt;

&lt;p&gt;The LINQ query shown here uses &lt;strong&gt;Descendants&lt;/strong&gt; to iterate over the elements in the &lt;strong&gt;FirstFourPlanets.xml&lt;/strong&gt; file. The &lt;strong&gt;where&lt;/strong&gt; filter in the query checks to see if any of those elements has its line number set to 21. It happens that the 15&lt;sup&gt;th&lt;/sup&gt; element returned by the call to &lt;strong&gt;Descendants&lt;/strong&gt; fits that search criteria, and so that node, and that node alone, is found when we &lt;strong&gt;foreach&lt;/strong&gt; over the results.&lt;/p&gt;

&lt;p&gt;Notice the cast to convert the &lt;strong&gt;XElement&lt;/strong&gt; nodes returned by the call to &lt;strong&gt;Descendants&lt;/strong&gt;:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;lineInfo = (IXmlLineInfo)x&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This cast is necessary, since the actual fields of the &lt;strong&gt;IXmlLineInfo&lt;/strong&gt; interface are not exposed by &lt;strong&gt;XElement&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Once again, I want to stress that reporting the line number of a node seems like a reasonable thing to do, but searching for an element by line number is usually not a good idea in production code. For unexplained reasons, code that was on line 532 has a way of migrating to line 533 when you least expect it. In any case, you now know enough to begin working with line numbers in a LINQ to XML program.&lt;/p&gt;

&lt;p&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=3251"&gt;source&lt;/a&gt;.&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f09%2f26%2flinq-farm-linq-to-xml-and-line-numbers.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f09%2f26%2flinq-farm-linq-to-xml-and-line-numbers.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8966999" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LinqFarm/default.aspx">LinqFarm</category></item><item><title>Charlie to Speak on LINQ at DevLink</title><link>http://blogs.msdn.com/charlie/archive/2008/08/18/charlie-on-linq-at-devlink.aspx</link><pubDate>Tue, 19 Aug 2008 08:12:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8877876</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8877876.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8877876</wfw:commentRss><description>&lt;p&gt;I will be speaking this Friday and Saturday, August 22nd and 23rd, at &lt;a href="http://www.devlink.net/"&gt;devLink&lt;/a&gt; near Nashville, TN. I will give two talks, both on LINQ, both for intermediate and advanced developers. The first covers querying in LINQ, and the second talk explains the features of C# 3.0 that make LINQ possible.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Essence of LINQ Part I&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Query expressions are the primary tool for writing LINQ queries. This talk will explore the syntax for writing query expressions. It will also explore a number of the most important LINQ operators. The 7 different types of LINQ clauses will be covered, as well as the 4 different ways to introduce range variables, the different means of creating a LINQ join, and the role of composition in LINQ. Some of the operators that will get special attention include Join, SelectMany, GroupBy, OrderBy and the Aggregate and Set operators. This talk digs into some intermediate and advanced issues, and I assume that most attendees know at least a little about LINQ.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Essence of LINQ Part II&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;To enable LINQ, C# 3.0 introduces a number of new language features such as query expressions, lambda expressions, extension methods, automatically implemented properties, local type inference, expression trees and more. These are all features that can improve the quality of your code. They also provide new opportunities for making mistakes This talk will explore these features and outline best practices for using them.. Each feature will be introduced with an example illustrating how it contributes to the overall LINQ architecture.  &lt;p&gt;The materials for my talk are on the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt; on &lt;a href="http://code.msdn.microsoft.com/"&gt;Code Gallery&lt;/a&gt;. To download them directly, click &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=2828"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8877876" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/Community/default.aspx">Community</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>Released: Visual Studio Service Pack 1; .NET 3.5 Service Pack 1</title><link>http://blogs.msdn.com/charlie/archive/2008/08/11/released-visual-studio-service-pack-1-net-3-5-service-pack-1.aspx</link><pubDate>Mon, 11 Aug 2008 21:03:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8848643</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>20</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8848643.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8848643</wfw:commentRss><description>&lt;p&gt;Visual Studio Service Pack 1 and .NET 3.5 Service Pack 1 are now available for download. A new feature for C# developers called squiggles provides better error messages while you are typing your code. There are several important LINQ performance enhancements, including one that could yield a 30% speed increase in some common scenarios. There are also thousands of other bug fixes.&lt;/p&gt;  &lt;p&gt;A general description of these releases is available &lt;a href="http://msdn.microsoft.com/en-us/vstudio/products/cc533447.aspx"&gt;here&lt;/a&gt;. It describes Visual Studio and .NET 3.5 changes such as:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Improvements to the WPF designers, and core WPF performance. &lt;/li&gt;    &lt;li&gt;SQL 2008 support &lt;/li&gt;    &lt;li&gt;Entity Framework support. &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Download &lt;/h3&gt;  &lt;p&gt;A download page is available &lt;a href="http://msdn.microsoft.com/en-us/vstudio/cc533448.aspx"&gt;here&lt;/a&gt;, or you can just follow these links:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;VS 2008 Service Pack 1&lt;/strong&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Executable: &lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=122094"&gt;http://go.microsoft.com/fwlink/?LinkId=122094&lt;/a&gt;&lt;/u&gt; (&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=FBEE1648-7106-44A7-9649-6D9F6D58056E&amp;amp;displaylang=en"&gt;Direct Link&lt;/a&gt;) &lt;/li&gt;    &lt;li&gt;Iso: &lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=122095"&gt;http://go.microsoft.com/fwlink/?LinkId=122095&lt;/a&gt;&lt;/u&gt; (&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=27673C47-B3B5-4C67-BD99-84E525B5CE61&amp;amp;displaylang=en"&gt;Direct Link&lt;/a&gt;) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;VS 2008 Express Editions with Service Pack 1&lt;/strong&gt;: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Executable: &lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=123679"&gt;http://go.microsoft.com/fwlink/?LinkId=123679&lt;/a&gt;&lt;/u&gt; &lt;/li&gt;    &lt;li&gt;ISO: &lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=123680"&gt;http://go.microsoft.com/fwlink/?LinkId=123680 &lt;/a&gt;&lt;/u&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;VS Team System 2008 Team Foundation SP 1&lt;/strong&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=124829"&gt;http://go.microsoft.com/fwlink/?LinkId=124829 &lt;/a&gt;&lt;/u&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;.NET Framework 3.5 Service Pack 1:&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=124150 "&gt;http://go.microsoft.com/fwlink/?LinkId=124150 &lt;/a&gt;&lt;/u&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f08%2f11%2freleased-visual-studio-service-pack-1-net-3-5-service-pack-1.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f08%2f11%2freleased-visual-studio-service-pack-1-net-3-5-service-pack-1.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8848643" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/charlie/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>LINQ Farm: More on the LINQ Aggregate Operators</title><link>http://blogs.msdn.com/charlie/archive/2008/07/24/linq-farm-more-on-aggregate-operators.aspx</link><pubDate>Fri, 25 Jul 2008 04:00:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8732428</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8732428.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8732428</wfw:commentRss><description>&lt;p&gt;The LINQ aggregate operators allow you to perform simple math operations over the elements in a sequence. This post is designed to walk you through those operators, and give you an overview of how to use them. Table 1 shows a list of the 7 aggregate operators.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: All the samples shown in this post are found in the &lt;b&gt;&lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=2623"&gt;AggregateOperators&lt;/a&gt;&lt;/b&gt; program found on &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;Code Gallery LINQ Farm&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Table 1: LINQ includes 7 aggregate operators designed to help you perform simple math operations. The definitions shown in this table are over-simplifications that give you a general sense of what you can do with a particular operator.&lt;/strong&gt;&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="2" width="557" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="102"&gt;Count &lt;/td&gt;        &lt;td valign="top" width="453"&gt;Count the elements in a sequence.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="104"&gt;LongCount &lt;/td&gt;        &lt;td valign="top" width="453"&gt;Count the elements in a very, very long sequence.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="105"&gt;Sum &lt;/td&gt;        &lt;td valign="top" width="453"&gt;Add the elements in a sequence&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="106"&gt;Min &lt;/td&gt;        &lt;td valign="top" width="453"&gt;Find the smallest element in a sequence&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="107"&gt;Max &lt;/td&gt;        &lt;td valign="top" width="453"&gt;Find the largest element in a sequence&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="108"&gt;Average &lt;/td&gt;        &lt;td valign="top" width="453"&gt;Find the average value in a sequence&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="108"&gt;Aggregate &lt;/td&gt;        &lt;td valign="top" width="453"&gt;Perform various binary operations on the elements in a sequence.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Except for the &lt;b&gt;Aggregate&lt;/b&gt; operator itself, all of these operators have a simple, obvious default use. Several of these operators, do, however, have overloads that need a few sentences of explanation. I will show you one simple example of using the default behavior for the operators, and then dive a bit deeper with a second example that shows how to use at least one of the overloads.&lt;/p&gt;  &lt;h2&gt;The Count and LongCount Operators&lt;/h2&gt;  &lt;p&gt;The &lt;b&gt;Count&lt;/b&gt; and &lt;b&gt;LongCount&lt;/b&gt; operators return the number of elements in a sequence. The &lt;b&gt;Count&lt;/b&gt; operator can find this number quickly by simply asking objects such as &lt;b&gt;List&lt;/b&gt;&amp;lt;T&amp;gt; that support the ICollection&amp;lt;T&amp;gt; interface for the count. If that service is not available, then LINQ iterates over the items in a list to get the count.&amp;#160; The &lt;b&gt;LongCount&lt;/b&gt; operator provides the same basic functionality, but allows you to work with an &lt;b&gt;Int64&lt;/b&gt;. A simple example of using the &lt;b&gt;Count&lt;/b&gt; operator is shown in Listing 1. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Listing 1: A simple example of using the Count and LongCount operators.&lt;/strong&gt;&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;ShowCount()
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;list = &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(5, 12);
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(list.Count());
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The overloads for &lt;b&gt;Count&lt;/b&gt; and &lt;b&gt;LongCount&lt;/b&gt; allow you to pass in a lambda expression that performs custom calculations from which LINQ can derive the count for a sequence. For instance, you can write code that returns the number of even numbers in a collection:&lt;/p&gt;

&lt;pre class="code"&gt;var list = &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(1, 25);&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Total Count: {0}, Count the even numbers: {1}&amp;quot;&lt;/span&gt;,
    list.Count(), list.Count(n =&amp;gt; n % 2 == 0));         &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Our list consists of the numbers between 1 and 25. We call count once with the first version of the Count operator and get back the number 25.&lt;/p&gt;

&lt;p&gt;The second overload of the &lt;b&gt;Count&lt;/b&gt; operator takes a simple predicate. The declaration looks like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static int &lt;/span&gt;Count&amp;lt;TSource&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; source,
   &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TSource, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; predicate);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The predicate takes an integer and returns a bool specifying whether or not a particular value from the list passes a test. In our case, we simple ask whether or not the number is even. This computation will return the values 2, 4, 6 and so on up to 24, for a total of 12 elements.&lt;/p&gt;

&lt;h2&gt;The Min and Max Operators&lt;/h2&gt;

&lt;p&gt;The &lt;b&gt;Min&lt;/b&gt; and &lt;b&gt;Max&lt;/b&gt; operators are equally simple. Listings 2 and 3 show how it works. The first shows the behavior of the first overload of &lt;strong&gt;Min&lt;/strong&gt; and &lt;strong&gt;Max&lt;/strong&gt;, the second shows how to one of the other overloads to pose slightly more complex questions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Listing 2: A simple example of using the Min and Max operators to determine and highest and lowest values in a sequence.&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;ShowMinMax()
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;list = &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(6, 10);
      &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Min: {0}, Max: {1}&amp;quot;&lt;/span&gt;, list.Min(), list.Max());
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Our list consists of the number 6 through 15, so the code writes out the values 6 and 15 to the console.The C# source that implements &lt;strong&gt;Min&lt;/strong&gt; and &lt;strong&gt;Max&lt;/strong&gt; use the &lt;b&gt;IComparable&lt;/b&gt;&amp;lt;T&amp;gt; or &lt;b&gt;IComparable&lt;/b&gt; interfaces to perform the calculations. If you pass in a null argument you will get an &lt;strong&gt;ArgumentNullException&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For the more complex examples, I'm going to need a few rows of simple data, which I provide in Listing 3. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Listing 3: The following Item class and the &lt;u&gt;GetItems&lt;/u&gt; method are used by most of the examples in this section of the text.&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Item
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;Width { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;Length { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public override string &lt;/span&gt;ToString()
    {
        &lt;span style="color: blue"&gt;return string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;&amp;quot;Width: {0}, Length: {1}&amp;quot;&lt;/span&gt;, Width, Length);
    }
}

&lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Item&lt;/span&gt;&amp;gt; GetItems()
{
    &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Item&lt;/span&gt;&amp;gt; 
    { 
       &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Item &lt;/span&gt;{ Length = 0, Width = 5 },
       &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Item &lt;/span&gt;{ Length = 1, Width = 6 },
       &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Item &lt;/span&gt;{ Length = 2, Width = 7 },
       &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Item &lt;/span&gt;{ Length = 3, Width = 8 },
       &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Item &lt;/span&gt;{ Length = 4, Width = 9 }
    };
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;There is no simple way to know maximum or minimum values from a list of &lt;strong&gt;Items&lt;/strong&gt;. To find the largest &lt;b&gt;Item&lt;/b&gt; do you choose the element with the greatest &lt;b&gt;Length&lt;/b&gt;, the greatest &lt;b&gt;Width, &lt;/b&gt;or some other value? To solve this problem the C# teams provided us with an overload of the &lt;strong&gt;Min&lt;/strong&gt; and &lt;strong&gt;Max&lt;/strong&gt; operators that take a delegate that we can use to select the proper value for the comparison:&lt;/p&gt;

&lt;p&gt;&lt;span style="color: blue"&gt;public static int &lt;/span&gt;Max&amp;lt;TSource&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; source, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TSource, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; selector);&lt;/p&gt;

&lt;p&gt;Like nearly all the LINQ to Objects operators, Max is implemented as an extension method for the class &lt;b&gt;IEnumerable&lt;/b&gt;&amp;lt;T&amp;gt;. It takes an extremely simple lambda that is passed an element from the enumeration and returns an integer. To see how this works, take a look at Listing 4.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Listing 4: A somewhat more complex use of Min and Max, demonstrating how to get minimum and maximum values for complex types with multiple fields.&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Item&lt;/span&gt;&amp;gt; items = GetItems();
ShowList(items);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;MinLength: {0}, MaxLength: {1}&amp;quot;&lt;/span&gt;, 
   items.Min(l =&amp;gt; l.Length ), items.Max(l =&amp;gt; l.Length));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, &lt;strong&gt;Min&lt;/strong&gt; and &lt;strong&gt;Max&lt;/strong&gt; both take a very simple delegate, which is implemented here as a lambda. &lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The lambda that is passed to &lt;strong&gt;Min&lt;/strong&gt; looks like this: &lt;b&gt;l =&amp;gt; l.Length&lt;/b&gt;. This is lambda is so simple that it can be a bit confusing to people who are new to LINQ. Let's take one moment to be sure we understand what is happening.&lt;/p&gt;

&lt;p&gt;We know that this LINQ operator must iterate over the sequence passed in to it, and we can assume that it passes each item it finds to the &lt;b&gt;selector&lt;/b&gt; delegate. It then tests the result returned from &lt;b&gt;selector&lt;/b&gt;, to see if it is the largest value returned. Without peeking at the real source code, it seems that &lt;b&gt;Max&lt;/b&gt; might do something like the code in listing 5.&lt;/p&gt;

&lt;p&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt;&lt;strong&gt;Listing 5: This method, which I created, mimics what occurs in the real Max method that ships with the C# 3.0 release.&lt;/strong&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;

&lt;pre&gt;&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyExtensions
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static int &lt;/span&gt;Max&amp;lt;TSource&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; source
      &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TSource, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; selector)
    {
        &lt;span style="color: blue"&gt;int &lt;/span&gt;largest = &lt;span style="color: blue"&gt;int&lt;/span&gt;.MinValue;
        &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;item &lt;span style="color: blue"&gt;in &lt;/span&gt;source)
        {
            &lt;span style="color: blue"&gt;int &lt;/span&gt;nextItem = selector(item);
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(nextItem &amp;gt; largest)
            {
                largest = nextItem;
            }
        }
        &lt;span style="color: blue"&gt;return &lt;/span&gt;largest;
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Assuming that we are working with a collection of &lt;b&gt;Items&lt;/b&gt;, then &lt;b&gt;selector&lt;/b&gt;, were it implemented as a standard method, would have to look something like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public int &lt;/span&gt;selector(&lt;span style="color: #2b91af"&gt;Item &lt;/span&gt;item)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;item.Length;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This method is semantically identical to the delegate we used in listing x: &lt;b&gt;l =&amp;gt; l.Length&lt;/b&gt;. It is very simple code that tells us which part of the &lt;b&gt;Item&lt;/b&gt; class we are going to use to determine our max value. &lt;/p&gt;

&lt;p&gt;It's all so simple that one feels a little like a character in Edgar Allan Poe's &amp;quot;The Purloined Letter:&amp;quot; the answer was hidden in plain sight. Once again we see that the biggest impediment to learning LINQ is the fear that it might be complicated. In practice, it is almost startlingly simple.&lt;/p&gt;

&lt;h2&gt;The Average Operator&lt;/h2&gt;

&lt;p&gt;Once one understands the pattern shown in our examination of the &lt;strong&gt;Min&lt;/strong&gt; and &lt;strong&gt;Max&lt;/strong&gt; operators, we find that it can be easily applied to most of the other &lt;strong&gt;Aggregate&lt;/strong&gt; operators. Let&amp;#8217;s look at the Average operator, which returns the average value from an enumeration.&lt;/p&gt;

&lt;p&gt;For instance, one can find the average for a range of numbers like this:&lt;/p&gt;

&lt;pre class="code"&gt;var list = &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(0, 5);
&lt;span style="color: #2b91af"&gt;  Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Average: {0}&amp;quot;&lt;/span&gt;, list.Average());&lt;/pre&gt;

&lt;p&gt;When run, this code tells us that the average of the numbers 0, 1, 2, 3, 4 is the value 2.&lt;/p&gt;

&lt;p&gt;When working with a collection of Items, we face the same problem we had with Min and Max: How does one discover the average value for list of Items that define two properties called Length and Width? The answer, of course, is that proceed just as we did with Min and Max operators:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Item&lt;/span&gt;&amp;gt; items = GetItems();&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;double &lt;/span&gt;averageLength = items.Average(l =&amp;gt; l.Length);
&lt;span style="color: blue"&gt;double &lt;/span&gt;averageWidth = items.Average(w =&amp;gt; w.Width);
&lt;span style="color: blue"&gt;double &lt;/span&gt;averageValue = items.Average(v =&amp;gt; v.Length + v.Width);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;AverageLength: {0}, AverageWidth: {1} AverageValue: {2}&amp;quot;&lt;/span&gt;, averageLength, averageWidth, averageValue);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Again, we pass in very simple lambdas such as &lt;strong&gt;l =&amp;gt; l.Length + l.Width&lt;/strong&gt; or w =&amp;gt; w.Width. Somewhere in the background code similar to what you see in the custom implementation for the Max operator found in listing 5.X. The code must iterate over the list, passing in each item to our lambda, which defines the value we want the Average operator to use in its calculations:&lt;/p&gt;

&lt;p&gt;AverageLength: 2, AverageWidth: 7 AverageValue: 9&lt;/p&gt;

&lt;h2&gt;The Sum Operator&lt;/h2&gt;

&lt;p&gt;The Sum operator tallies the values in an enumeration. Consider the following simple example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;list = &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(5, 3);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;List sum = {0}&amp;quot;&lt;/span&gt;, list.Sum());&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Our list consists of the numbers 5, 6 and 7. The Sum operator adds them together, producing the value 18.&lt;/p&gt;

&lt;p&gt;working with a list of Items, the Sum operator faces the same problem we saw with the Min, Max and Average operators. It should come as no surprise that the solution is nearly identical:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;items = GetItems(); 
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Sum the lengths of the items: {0}&amp;quot;&lt;/span&gt;, items.Sum(l =&amp;gt; l.Length));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here is the same pattern you saw with the &lt;strong&gt;Average&lt;/strong&gt;, &lt;strong&gt;Min&lt;/strong&gt; and &lt;strong&gt;Max&lt;/strong&gt; operators: we pass in a simple lambda to help the Sum method know which part of an Item it should use as the operand when performing its simple addition. The result printed to the console is the value 10. If only the rest of our lives were quite this simple!&lt;/p&gt;

&lt;h2&gt;The Aggregate Operator&lt;/h2&gt;

&lt;p&gt;The &lt;b&gt;Aggregate&lt;/b&gt; operator follows in the footsteps of the &lt;b&gt;Sum&lt;/b&gt; operator, but it provides us with a few more options. Rather than taking a simple delegate like the other operators in this series, it asks for one similar to the lambda we worked with in &lt;a href="http://blogs.msdn.com/charlie/archive/2008/06/28/lambdas.aspx"&gt;a previous post&lt;/a&gt;:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;T Aggregate&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; source, &lt;font color="#2b91af"&gt;Func&lt;/font&gt;&amp;lt;T, T, T&amp;gt; func);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We know what do to with delegates that looks like this. We could, for instance, create one that adds up a range of numbers:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;list = &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(5, 3);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Aggregation: {0}&amp;quot;&lt;/span&gt;, list.Aggregate((a, b) =&amp;gt; (a + b)));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The aggregate operator gets passed the numbers 5, 6 and 7. The first time the lambda is called it gets passed 5 and 6, and adds them together to produce 11. The next time it is called it is passed the accumulated result of the previous calculation plus the next number in the series: (11 + 7) which yields 18. This is the same result we saw for the &lt;strong&gt;Sum&lt;/strong&gt; operator in the previous section. This overload of the &lt;strong&gt;Aggregate&lt;/strong&gt; operator is indeed very similar to the &lt;strong&gt;Sum&lt;/strong&gt; operator, though it is more flexible, in that you can easily perform multiplication, division, subtraction and other operations instead of simple addition. For instance, this code performs multiplication, yielding the value 210: &lt;/p&gt;

&lt;p&gt;list.Aggregate((a, b) =&amp;gt; (a * b))&lt;/p&gt;

&lt;p&gt;Before pushing on, I should backtrack a little and discuss two simple points that are often brought up when people talk about this first version of the Aggregate operator. If it is passed a list with one item, it returns that item. If it is passed a list with 0 items, it throws an &lt;b&gt;InvalidOperationException&lt;/b&gt;.&lt;/p&gt;

&lt;p&gt;A second overload of the &lt;b&gt;Aggregate&lt;/b&gt; operator allows you to seed the process with an accumulator:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;TAccumulate Aggregate&amp;lt;TSource, TAccumulate&amp;gt;(
    &lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; source, TAccumulate seed, 
    &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TAccumulate, TSource, TAccumulate&amp;gt; func);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;This is essentially the same operator as shown in the previous example, but now you can decide the starting point for the value that will be accumulated:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Aggregation: {0}&amp;quot;&lt;/span&gt;, list.Aggregate(0, (a, b) =&amp;gt; (a + b)));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If we pass in a list with one item in it, say the number five, then the first time the lambda is called it would be passed the seed plus the sole item in the list:&lt;/p&gt;

&lt;p&gt;(0 + 5)&lt;/p&gt;

&lt;p&gt;The result, of course, is the number 5. &lt;/p&gt;

&lt;p&gt;Suppose we pass in an accumulator of 0 plus the numbers 5, 6, 7. &lt;/p&gt;

&lt;p&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;list = &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(5, 3); 

  &lt;br /&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Aggregation: {0}&amp;quot;&lt;/span&gt;, list.Aggregate(0, (a, b) =&amp;gt; (a + b)));&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;In this case we would step through the following sequence:&lt;/p&gt;

&lt;pre&gt;0 + 5 = 5
5 + 6 = 11
11 + 7 = 18.&lt;/pre&gt;

&lt;p&gt;Again, we are doing essentially what we did with the &lt;strong&gt;Sum&lt;/strong&gt; operator.&lt;/p&gt;

&lt;p&gt;If you pass in a different seed, then you get a different result:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Aggregation: {0}&amp;quot;&lt;/span&gt;, list.Aggregate(3, (a, b) =&amp;gt; (a + b)));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;With a seed of 3, we get:&lt;/p&gt;

&lt;pre&gt;3 + 5 = 8
8 + 6 = 14
14 + 7 = 21&lt;/pre&gt;

&lt;p&gt;As mentioned earlier, the Aggregate operator allows us to perform not just addition, but multiplication, division or various other binary mathematical operations:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Aggregation: {0}&amp;quot;&lt;/span&gt;, list.Aggregate(1, (a, b) =&amp;gt; (a * b)));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;In this case the series looks like this:&lt;/p&gt;

&lt;pre&gt;1 * 5 = 5
5 * 6 = 30
30 * 7 = 210&lt;/pre&gt;

&lt;p&gt;Note that I passed in an accumulator equal to 1, so that we did not end up with the following series of operations:&lt;/p&gt;

&lt;pre&gt;0 * 5 = 0
0 * 6 = 0
0 * 7 = 0&lt;/pre&gt;

&lt;p&gt;In what I sometimes suspect might have been an excess of good spirits, the team added one final overload to the Aggregate operator:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;TResult Aggregate&amp;lt;TSource, TAccumulate, TResult&amp;gt;(
                &lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; source, TAccumulate seed,
                &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TAccumulate, TSource, TAccumulate&amp;gt; func,
                &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TAccumulate, TResult&amp;gt; resultSelector);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This overload is nearly identical to the previous overload, but you are given one more, very simple, delegate that you can use to transform the result of your aggregation. For instance, consider this use of the Aggregate operator:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Aggregation: {0}&amp;quot;&lt;/span&gt;, list.Aggregate(0, (a, b) =&amp;gt; (a + b),
    (a) =&amp;gt; (&lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;&amp;quot;{0:C}&amp;quot;&lt;/span&gt;, a))));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Please notice that the first two-thirds of this call mirror what we did earlier, and only the third parameter is new.&lt;/p&gt;

&lt;p&gt;Suppose we pass in a sequence with the values 5, 6 and 7. As we've already seen, the process will begin by performing the following series of operations:&lt;/p&gt;

&lt;pre&gt;0 + 5 = 5
5 + 6 = 11
11 + 7 = 18&lt;/pre&gt;

&lt;p&gt;Once we have our result of 18, this number is passed to the last lambda in our call. It uses the string's &lt;strong&gt;Format&lt;/strong&gt; method to transform it into a string in currency format:&lt;/p&gt;

&lt;p&gt;$18.00&lt;/p&gt;

&lt;p&gt;Like nearly everything in LINQ, this seems terribly complicated at first only to end up being reasonably simple. It is these kinds of simple operations, however, which provide us with the building blocks out of which we can safely create complex programs. This is what we mean when we apply the word elegant to a technology.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: All the samples shown in this post are found in the &lt;b&gt;&lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=2623"&gt;AggregateOperators&lt;/a&gt;&lt;/b&gt; program found on &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;Code Gallery LINQ Farm&lt;/a&gt;.&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f07%2f14%2flinq-farm-more-on-aggregate-operators.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f07%2f14%2flinq-farm-more-on-aggregate-operators.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8732428" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LinqFarm/default.aspx">LinqFarm</category></item><item><title>LINQ Farm: More on Set Operators</title><link>http://blogs.msdn.com/charlie/archive/2008/07/12/the-linq-set-operators.aspx</link><pubDate>Sat, 12 Jul 2008 10:47:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8723104</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8723104.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8723104</wfw:commentRss><description>&lt;p&gt;This is a second post on the LINQ Set operators, the first being published while LINQ was still in beta. As mentioned in &lt;a href="http://blogs.msdn.com/charlie/archive/2006/12/28/linq-farm-linq-sets.aspx"&gt;the previous post&lt;/a&gt;, there are four LINQ set operators: &lt;strong&gt;Union&lt;/strong&gt;, &lt;strong&gt;Intersect&lt;/strong&gt;, &lt;strong&gt;Distinct&lt;/strong&gt; and &lt;strong&gt;Except&lt;/strong&gt;. Like the other 49 LINQ operators, these methods are designed to allow you to query data which supports the &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt; interface. Since all LINQ query expressions, and most LINQ queries, return &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt;, these operators are designed to allow you to perform set operations on the results of a LINQ query. &lt;/p&gt; &lt;p&gt;In this post I give four highly simplified examples of how to use each of the operators, and then end with a more complex example that shows how the operators might be used in a real world setting.&lt;/p&gt; &lt;p&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=2564"&gt;source code&lt;/a&gt;.&lt;/p&gt; &lt;h2&gt;Union&lt;/h2&gt; &lt;p&gt;The &lt;strong&gt;Union&lt;/strong&gt; operator shows the unique items from two lists, as shown in listing 1.  &lt;p&gt;&lt;strong&gt;Listing 1: The Show Union method displays the number 1, 2, 3, 4, 5 and 6.&lt;/strong&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; ShowUnion()
{
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listA = &lt;span style="color: rgb(43,145,175)"&gt;Enumerable&lt;/span&gt;.Range(1, 3);
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listB = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; { 3, 4, 5, 6 };

    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listC = listA.Union(listB);

    &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; listC)
    {
        &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(item);
    }
} &lt;/pre&gt;
&lt;p&gt;Here two collections are joined together, but only the unique members of each list are retained.&lt;/p&gt;
&lt;h2&gt;Intersect&lt;/h2&gt;
&lt;p&gt;The &lt;strong&gt;Intersect&lt;/strong&gt; operator shows the items that two lists have in common. 
&lt;p&gt;&lt;strong&gt;Listing 2: The ShowIntersect method displays the numbers 3 and 4&lt;/strong&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; ShowIntersect()
{
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listA = &lt;span style="color: rgb(43,145,175)"&gt;Enumerable&lt;/span&gt;.Range(1, 4);
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listB = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; { 3, 4, 5, 6 };

    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listC = listA.Intersect(listB);

    &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; listC)
    {
        &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(item);
    }
}       
&lt;/pre&gt;
&lt;p&gt;Here to collections are joined together, and only the unique, shared members of each list are retained.&lt;/p&gt;
&lt;h2&gt;Distinct&lt;/h2&gt;
&lt;p&gt;The &lt;strong&gt;Distinct&lt;/strong&gt; operator finds all the unique items in a list. 
&lt;p&gt;&lt;strong&gt;Listing 3: The ShowDistinct method displays the number 1, 2 and 3.&lt;/strong&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; ShowDistinct() 
{
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listA = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; { 1, 2, 3, 3, 2, 1 };
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listB = listA.Distinct();

    &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; listB)
    {
        &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(item);
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;h2&gt;Except&lt;/h2&gt;
&lt;p&gt;The &lt;b&gt;Except&lt;/b&gt; operator shows all the items in one list minus the items in a second list.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Listing 4: The ShowExcept method prints out the numbers 1, 2, 5, and 6&lt;/strong&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; ShowExcept() 
{
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listA = &lt;span style="color: rgb(43,145,175)"&gt;Enumerable&lt;/span&gt;.Range(1, 6);
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listB = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; { 3, 4 };

    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listC = listA.Except(listB);

    &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; listC)
    {
        &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(item);
    }
}
&lt;/pre&gt;
&lt;h2&gt;In the Context of LINQ&lt;/h2&gt;
&lt;p&gt;The type of code listed above is useful, but it might be helpful to see these same operators used in the context of a LINQ query expression. You can then see how they can be used to analyze the results of queries to better understand the data that is returned. &lt;/p&gt;
&lt;p&gt;You probably know that there are two similar collections used to create lists. One is the generic &lt;strong&gt;List&lt;/strong&gt;&amp;lt;&lt;strong&gt;T&lt;/strong&gt;&amp;gt; collection and the other is the old-style collection called &lt;strong&gt;ArrayList&lt;/strong&gt;. We can use set operators to help us better understand the difference between these two classes.&lt;/p&gt;
&lt;p&gt;Here are two queries retrieving the methods from the&lt;strong&gt; List&amp;lt;int&amp;gt;&lt;/strong&gt; class and the &lt;strong&gt;ArrayList&lt;/strong&gt; class:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; queryList = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; m &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt;).GetMethods()
                &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; m.DeclaringType == &lt;span style="color: rgb(0,0,255)"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt;)
                &lt;span style="color: rgb(0,0,255)"&gt;group&lt;/span&gt; m &lt;span style="color: rgb(0,0,255)"&gt;by&lt;/span&gt; m.Name &lt;span style="color: rgb(0,0,255)"&gt;into&lt;/span&gt; g
                &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; g.Key;

&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; queryArray = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; m &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;ArrayList&lt;/span&gt;).GetMethods()
                 &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; m.DeclaringType == &lt;span style="color: rgb(0,0,255)"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;ArrayList&lt;/span&gt;)
                 &lt;span style="color: rgb(0,0,255)"&gt;group&lt;/span&gt; m &lt;span style="color: rgb(0,0,255)"&gt;by&lt;/span&gt; m.Name &lt;span style="color: rgb(0,0,255)"&gt;into&lt;/span&gt; g
                 &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; g.Key;
&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Here is code to retrieve the interesection of these two lists: &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listIntersect = queryList.Intersect(queryArray);&lt;/pre&gt;
&lt;p&gt;And here is code that displays the resulting sequence:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"Count: {0}"&lt;/span&gt;, listIntersect.Count());&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; listIntersect)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(item);&lt;br&gt;}&lt;/pre&gt;
&lt;p&gt;Alternatively, you could write the query like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; queryList = (&lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; m &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt;).GetMethods()
                 &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; m.DeclaringType == &lt;span style="color: rgb(0,0,255)"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt;)
                 &lt;span style="color: rgb(0,0,255)"&gt;group&lt;/span&gt; m &lt;span style="color: rgb(0,0,255)"&gt;by&lt;/span&gt; m.Name &lt;span style="color: rgb(0,0,255)"&gt;into&lt;/span&gt; g
                 &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; g.Key).Intersect(&lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; m &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;ArrayList&lt;/span&gt;).GetMethods()
                                         &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; m.DeclaringType == &lt;span style="color: rgb(0,0,255)"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;ArrayList&lt;/span&gt;)
                                         &lt;span style="color: rgb(0,0,255)"&gt;group&lt;/span&gt; m &lt;span style="color: rgb(0,0,255)"&gt;by&lt;/span&gt; m.Name &lt;span style="color: rgb(0,0,255)"&gt;into&lt;/span&gt; g
                                         &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; g.Key);
&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;In either case, the following list would be displayed:&lt;/p&gt;
&lt;p&gt;get_Capacity&lt;br&gt;set_Capacity&lt;br&gt;get_Count&lt;br&gt;get_Item&lt;br&gt;set_Item&lt;br&gt;Add&lt;br&gt;AddRange&lt;br&gt;BinarySearch&lt;br&gt;Clear&lt;br&gt;Contains&lt;br&gt;CopyTo&lt;br&gt;GetEnumerator&lt;br&gt;GetRange&lt;br&gt;IndexOf&lt;br&gt;Insert&lt;br&gt;InsertRange&lt;br&gt;LastIndexOf&lt;br&gt;Remove&lt;br&gt;RemoveAt&lt;br&gt;RemoveRange&lt;br&gt;Reverse&lt;br&gt;Sort&lt;br&gt;ToArray 
&lt;p&gt;And here is how to see the items that the generic lists supports that are not part of the old style collection:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; listDifference = queryList.Except(listIntersect);&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;And here is the result of this query:&lt;/p&gt;
&lt;p&gt;ConvertAll&lt;br&gt;AsReadOnly&lt;br&gt;Exists&lt;br&gt;Find&lt;br&gt;FindAll&lt;br&gt;FindIndex&lt;br&gt;FindLast&lt;br&gt;FindLastIndex&lt;br&gt;ForEach&lt;br&gt;RemoveAll&lt;br&gt;TrimExcess&lt;br&gt;TrueForAll 
&lt;p&gt;Now you have a list of the methods the two classes share in common, and a list showing what the new generic class has that is not part of the older collection. The LINQ set operators made it easy for you to discover this information. &lt;/p&gt;
&lt;p&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=2564"&gt;source code&lt;/a&gt;.&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f07%2f12%2fthe-linq-set-operators.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f07%2f12%2fthe-linq-set-operators.aspx" border="0"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8723104" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LinqFarm/default.aspx">LinqFarm</category></item><item><title>LINQ Farm: Lambdas</title><link>http://blogs.msdn.com/charlie/archive/2008/06/28/lambdas.aspx</link><pubDate>Sat, 28 Jun 2008 11:35:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8663374</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>34</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8663374.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8663374</wfw:commentRss><description>&lt;p&gt;Lambdas are a simple technology with an intimidating name. They sound like they are going to be difficult to understand, but in practice prove to be relatively trivial.  &lt;p&gt;LINQ has an almost inordinate need for its users to declare a large number of small, simple delegates. The architects of C# decided that forcing the users of LINQ to declare delegates using standard C# 2.0 delegate syntax was an overly verbose option. They wanted to find a shorter, more concise way to accomplish the same task.  &lt;p&gt;The syntax they settled on looks like this:  &lt;p&gt;&lt;span style="color: rgb(43,145,175)"&gt;Func&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;int, &lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;int, &lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;int&amp;gt; myLambda = (a, b) =&amp;gt; (a + b);&lt;/span&gt;&lt;/p&gt; &lt;p&gt;This is a shorthand way of writing code which is semantically equivalent to the following:&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; Add(&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; a, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; b)
{
    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; a + b;
}

&lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; myDelegate = Add;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;In the next few paragraphs I will compare these two ways of creating a delegate instance, and explain how they map back and forth. 
&lt;p&gt;It is obvious that the left hand side of the following two code fragments have the same meaning:&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; myLambda = (a, b) =&amp;gt; (a + b);
&lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; myDelegate = Add;
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;But how can the right hand side be the same? 
&lt;p&gt;It turns out that that the expression on the right hand side of the first statement is a shorthand way of writing a method semantically equivalent to the &lt;b&gt;Add&lt;/b&gt; method. Just to be clear, a lambda is not a reference to the &lt;b&gt;Add&lt;/b&gt; method, it is second method that does the same thing as the &lt;b&gt;Add&lt;/b&gt; method. 
&lt;p&gt;Here is the lambda: 
&lt;p&gt;(a, b) =&amp;gt; (a + b); 
&lt;p&gt;And here is the Add method:&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; Add(&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; a, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; b)
{
    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; a + b;
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Here is the place in the &lt;b&gt;Add&lt;/b&gt; method where we define the parameters it will take: 
&lt;p&gt;(int a, int b) 
&lt;p&gt;Here is the place in the lambda where we define the parameters that it will take: 
&lt;p&gt;(a, b) 
&lt;p&gt;Here is the place in the &lt;b&gt;Add&lt;/b&gt; method where we define what it will return: 
&lt;p&gt;return a + b; 
&lt;p&gt;Here is the place in the lambda where we define what it will return: 
&lt;p&gt;(a + b) 
&lt;p&gt;As you can see, a lambda and a method do the same thing: they define a set of parameters and an executable body of code. 
&lt;p&gt;The type declarations for a lambda are resolved using a technique very similar to the one we employ for generic methods and generic delegates. To see how this works, look again at the full declaration for the lambda:&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; myLambda = (a, b) =&amp;gt; (a + b);&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The generic delegate &lt;b&gt;Func&lt;/b&gt; says that the method being implemented takes two integers as parameters, and returns an integer. The compiler takes this information and applies it to the lambda. Behind the scenes it resolves (a, b) to (int a, int b) and defines the function such that the body of the lambda (a + b) returns an integer. Thus we give the compiler enough information to convert our lambda into a method that performs the same action as the &lt;b&gt;Add&lt;/b&gt; method. 
&lt;p&gt;The =&amp;gt; symbol is called a lambda operator and is usually pronounced “goes to.” Thus the lambda above can be read as “&lt;b&gt;a&lt;/b&gt; comma &lt;b&gt;b&lt;/b&gt; goes to &lt;b&gt;a&lt;/b&gt; plus &lt;b&gt;b&lt;/b&gt;,” or “&lt;b&gt;a&lt;/b&gt; and &lt;b&gt;b&lt;/b&gt; goes to &lt;b&gt;a&lt;/b&gt; plus &lt;b&gt;b&lt;/b&gt;.” 
&lt;p&gt;Though you will rarely need to do so, you can explicitly declare the type of parameters to a lambda expression: &lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; f = (&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; a, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; b) =&amp;gt; (a + b);&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;For void functions that do not return a value, just use an empty set of parenthesis: 
&lt;p&gt;() =&amp;gt; Console.WriteLine(); 
&lt;p&gt;Lambdas have access to local variables:&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; UseLocal()
{
    &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; n;
    &lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; func = () =&amp;gt; { n = 6; &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; n; };
    n = func();
    &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(n); &lt;span style="color: rgb(0,128,0)"&gt;// Outputs the number 6
&lt;/span&gt;}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;You might be familiar with anonymous methods from C# 2.0. Semantically, anonymous methods and lambdas are identical, but the lambda syntax is easier to use. As a result, there is probably no reason for you to use anonymous methods in the future. Below is a lambda and anonymous method side by side:&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; myLambda = (a, b) =&amp;gt; (a + b);
&lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; myAnonMethod = &lt;span style="color: rgb(0,0,255)"&gt;delegate&lt;/span&gt;(&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; a, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; b)
{
    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; a + b;
};
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Both methods take two integers, add them together, and return the result. Commenting further on anonymous methods at this point would serve no purpose, since lambdas create the same result with less work. 
&lt;p&gt;In this post you have had a chance to look at lambdas. Their name is intimidating, and their syntax can be a bit confusing at first. Once you see beneath the facade however, this technology turns out to be relatively easy to master. &lt;span style="color: rgb(0,0,255)"&gt;&lt;/p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/span&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f06%2f28%2flambdas.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f06%2f28%2flambdas.aspx" border="0"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8663374" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LinqFarm/default.aspx">LinqFarm</category></item><item><title>LINQ Farm: Extension Methods and Scoping</title><link>http://blogs.msdn.com/charlie/archive/2008/06/28/extension-methods-and-scoping.aspx</link><pubDate>Sat, 28 Jun 2008 11:17:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8663357</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8663357.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8663357</wfw:commentRss><description>&lt;p&gt;There are a few scoping rules that you must keep in mind when using extensions methods. Problems with scoping and extensions methods are rare, but when you encounter them they are quite vexing. &lt;/p&gt; &lt;p&gt;An instance method will always be called before an extension method. The runtime looks first for an instance method, if it finds an instance method with the right name and signature, it executes it and never looks for your extension method. The following code illustrates this problem:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; System;

&lt;span style="color: rgb(0,0,255)"&gt;namespace&lt;/span&gt; ConsoleApplication1
{
    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyClass
&lt;/span&gt;    {
        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; DoThis()
        {
            &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"MyClass.DoThis"&lt;/span&gt;);
        }
    }


    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyExtensions01
&lt;/span&gt;    {
        &lt;span style="color: rgb(0,128,0)"&gt;// Can never be called as if it were an instance method of MyClass.
&lt;/span&gt;        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; DoThis(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyClass&lt;/span&gt; myClass)
        {
            &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"MyExtensions01.DoThis"&lt;/span&gt;);
        }
    }


    &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Program
&lt;/span&gt;    {
        &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt;[] args)
        {
            &lt;span style="color: rgb(43,145,175)"&gt;MyClass&lt;/span&gt; myClass = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyClass&lt;/span&gt;();
            myClass.DoThis();                &lt;span style="color: rgb(0,128,0)"&gt;// Calls MyClass.DoThis&lt;/span&gt;
            &lt;span style="color: rgb(43,145,175)"&gt;MyExtensions01&lt;/span&gt;.DoThis(myClass);  &lt;span style="color: rgb(0,128,0)"&gt;// Calls MyExtensions01.DoThis&lt;/span&gt;
        }
    }
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;MyExtensions01.DoThis is a valid extension method for MyClass. However, it will never be called because MyClass.DoThis always takes precedence over it unless you explicitely call it as a static method of MyExtensions01.&lt;/p&gt;
&lt;p&gt;In cases where you have two extension methods with the same name, an extension method in the current namespace will win out over one in another namespace. Ambiguity will become an issue, however, when you try to call two extension methods with the same name and signature in the same namespace, or in two different namespaces both used by the current namespace. See Listing 5 for an example of this problem. &lt;/p&gt;
&lt;p&gt;&lt;span style="color: rgb(0,0,0)"&gt;The following code will not compile because the compiler finds the call to DoThat ambiguous: &lt;br&gt;&lt;/span&gt;&lt;span style="color: rgb(0,0,0)"&gt;&lt;br&gt;&lt;/span&gt;&lt;span style="color: rgb(0,0,0)"&gt;&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&lt;/span&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; System;
&lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; System.Linq;

&lt;span style="color: rgb(0,0,255)"&gt;namespace&lt;/span&gt; ExtensionScope
{
    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyClass
&lt;/span&gt;    {
        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; DoThis()
        {
            &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"Do this"&lt;/span&gt;);
        }
    }
}

&lt;span style="color: rgb(0,0,255)"&gt;namespace&lt;/span&gt; Extensions01
{
    &lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; ExtensionScope;

    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyExtensions01
&lt;/span&gt;    {
        &lt;span style="color: rgb(0,128,0)"&gt;// Can never be called
&lt;/span&gt;        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; DoThis(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyClass&lt;/span&gt; myClass)
        {
            &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"Do this"&lt;/span&gt;);
        }

        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; DoThat(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyClass&lt;/span&gt; myClass)
        {
            &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"Do bop"&lt;/span&gt;);
        }
    }
}

&lt;span style="color: rgb(0,0,255)"&gt;namespace&lt;/span&gt; Extensions02
{
    &lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; ExtensionScope;

    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyExtensions02
&lt;/span&gt;    {
        &lt;span style="color: rgb(0,128,0)"&gt;// Can never be called
&lt;/span&gt;        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; DoThis(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyClass&lt;/span&gt; myClass)
        {
            &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"Do this"&lt;/span&gt;);
        }

        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; DoThat(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyClass&lt;/span&gt; myClass)
        {
            &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"Do bang"&lt;/span&gt;);
        }
    }
}

&lt;span style="color: rgb(0,0,255)"&gt;namespace&lt;/span&gt; ExtensionScope
{
    &lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; Extensions01;
    &lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; Extensions02;

    &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Program
&lt;/span&gt;    {
        &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt;[] args)
        {
            &lt;span style="color: rgb(43,145,175)"&gt;MyClass&lt;/span&gt; m = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyClass&lt;/span&gt;();
            m.DoThat();
        }
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;This program throws a compile time error because the compiler does not know if you want to &lt;b&gt;MyExtionsions01.DoThat()&lt;/b&gt; or&lt;b&gt; MyExtension02.DoThat()&lt;/b&gt;. There are two ways to resolve this error: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You could remove the using directive for either &lt;strong&gt;Extensions01&lt;/strong&gt; or &lt;strong&gt;Extensions02&lt;/strong&gt;. In this case, that would be a fine resolution, but if there were other methods or classes in both Extensions01 and Extensions02 that you wanted to use, then this could become a painful, or even unacceptable, choice. 
&lt;li&gt;You could explicitly state which method you want to call using standard static syntax: &lt;strong&gt;MyExtensions01.DoThat(m)&lt;/strong&gt;. 
&lt;li&gt;You could move either &lt;strong&gt;MyExtensions02&lt;/strong&gt; or &lt;strong&gt;MyExtensions01&lt;/strong&gt; into the &lt;strong&gt;ExtensionScope &lt;/strong&gt;namespace:&amp;nbsp; &lt;/li&gt;&lt;/ul&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;namespace ExtensionScope
&lt;/span&gt;&lt;span style="color: rgb(0,0,0)"&gt;{
    &lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;public &lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;static &lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;class &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;MyExtensions02
    {
        &lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;public &lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;static &lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;void DoThat(&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;MyClass myClass)
        {
            &lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;Console.WriteLine(&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;"MyExtensions02.DoThat");
        }
    }
&lt;/span&gt;&lt;span style="color: rgb(0,0,0)"&gt;}
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This latter solution works so long as you have access to the source. &lt;/p&gt;
&lt;p&gt;It should be clear that some of the issues discussed here can lead to trouble if you are not careful. In particular, you don't want to end up in a situation where forcing someone to remove a namespace results in them losing access to important functionality, nor do you want to force them to choose between functionality they desire and using your extensions. &lt;/p&gt;
&lt;p&gt;It can also be a serious nuisance if you muddy a namespace with what many developers might consider superfluous methods. If you added 50 extension methods to the C# &lt;b&gt;string&lt;/b&gt; class, then developers who just want to access the base functionality of that object would always have to contend with your methods, particularly when using IntelliSense. &lt;/p&gt;
&lt;p&gt;To avoid or at least mitigate the seriousness of these problems, you should always place your extension methods in unique namespace separated from the rest of your code so that you can easily include or exclude the extension methods from a program. Listings 10 and 11 illustrate this technique. &lt;/p&gt;
&lt;p&gt;Place your extensions in a separate file, and give them a unique namespace:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;namespace&lt;/span&gt; MyCode
{
    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MyCode
&lt;/span&gt;    {
        &lt;span style="color: rgb(0,128,0)"&gt;// Code omitted here
&lt;/span&gt;    }
   
}&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;namespace&lt;/span&gt; MyCode.Extensions
{
    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;SpecialString
&lt;/span&gt;    {
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt;[] stateCodes = 
                {&lt;span style="color: rgb(163,21,21)"&gt;"AL"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"AK"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"AZ"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"AR"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"CA"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"CO"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"CT"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"DE"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"FL"&lt;/span&gt;,
                 &lt;span style="color: rgb(163,21,21)"&gt;"GA"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"HI"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"ID"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"IL"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"IN"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"IA"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"KS"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"KY"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"LA"&lt;/span&gt;,
                 &lt;span style="color: rgb(163,21,21)"&gt;"ME"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"MD"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"MA"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"MI"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"MN"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"MS"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"MO"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"MT"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"NE"&lt;/span&gt;,
                 &lt;span style="color: rgb(163,21,21)"&gt;"NV"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"NH"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"NJ"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"NM"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"NY"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"NC"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"ND"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"OH"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"OK"&lt;/span&gt;,
                 &lt;span style="color: rgb(163,21,21)"&gt;"OR"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"PA"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"RI"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"SC"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"SD"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"TN"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"TX"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"UT"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"VT"&lt;/span&gt;,
                 &lt;span style="color: rgb(163,21,21)"&gt;"VA"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"WA"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"WV"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"WI"&lt;/span&gt;,&lt;span style="color: rgb(163,21,21)"&gt;"WY"&lt;/span&gt;};

        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; IsState01(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; source)
        {
            &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (source == &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;) &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;;
            source = source.ToUpper(); 
            &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; stateCodes)
            {
                &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (source == item)
                {
                    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;true&lt;/span&gt;;
                }
            }
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;;
        }

        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; IsState02(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; source)
        {
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; (source == &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;) ? &lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt; : stateCodes.Contains(source.ToUpper());
        }
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;In this code I show you two alternative ways to implement the &lt;strong&gt;IsState&lt;/strong&gt; extension method. The second, which uses LINQ, is probably easier to maintain. You can access the extension methods in a namespace MyCode.Extensions like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; System;
&lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; MyCode;
&lt;span style="color: rgb(0,0,255)"&gt;using&lt;/span&gt; MyCode.Extensions;

&lt;span style="color: rgb(0,0,255)"&gt;namespace&lt;/span&gt; ConsoleApplication1
{
    &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Program
&lt;/span&gt;    {            
        &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt;[] args)
        {
            MyCode myCode = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; MyCode();
            &lt;span style="color: rgb(0,128,0)"&gt;// Use My Code here.
&lt;/span&gt;            &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; test = &lt;span style="color: rgb(163,21,21)"&gt;"WA"&lt;/span&gt;;
            &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (test.IsState02())
            {
                &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"{0} is a state"&lt;/span&gt;, test);
            }
        }
    }
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;In this code your extension method is available and the code compiles. Comment out the third using statement and your extension method would not be available and the code would not compile. The developer would, however, still have access to the functionality found in the &lt;b&gt;MyCode&lt;/b&gt; namespace. You could perhaps improve this technology by putting your extensions in their own assembly with its own namespace. You could then be sure that developers could choose to include or exclude the extra weight of your extension methods when they ship their code. &lt;/p&gt;
&lt;p&gt;Though extension methods are particularly useful in LINQ, they are now a part of the language, and if used with caution, they can be useful. Placing them in their own namespace is a best practice that should help you get the most from this feature. &lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f06%2f28%2fextension-methods-and-scoping.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f06%2f28%2fextension-methods-and-scoping.aspx" border="0"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8663357" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LinqFarm/default.aspx">LinqFarm</category></item><item><title>Cut Development Time: Use LINQ</title><link>http://blogs.msdn.com/charlie/archive/2008/06/16/cut-development-time-use-linq.aspx</link><pubDate>Tue, 17 Jun 2008 01:37:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8607571</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>18</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8607571.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8607571</wfw:commentRss><description>&lt;p&gt;When Microsoft employees talk about LINQ publicly, we haven't tended to emphasize how much time you can save by using it. This is perhaps because we don't want LINQ to be labeled as simply another RAD tool designed to save time. Nevertheless, it is becoming clear to me that shorter development cycles may be one of the first major benefits of LINQ to be widely recognized by the community.&lt;/p&gt;  &lt;p&gt;I first began to notice the importance of this issue a month or two ago at a customer visit in San Diego. I asked if anyone at the company we were visiting was using LINQ. Only one person raised their hand. I asked him about his experience, and one of the first things he said was that he was able to get a lot of work done very quickly. DBA's at our session immediately chimed in with the usual objections about moving queries out of the database and into a codebase, and from there the discussion wandered off on a predictable tangent. Yet since then I've remembered that user's experience with rapid development, and the obvious enthusiasm he had for his subject.&lt;/p&gt;  &lt;p&gt;The subject came up again during other stops in Southern California. The same point was raised by several developers I spoke with at Tech Ed, and again in &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc500645.aspx?pr=blog"&gt;a recent MSDN article&lt;/a&gt; by &lt;a href="http://jamesmccaffrey.spaces.live.com/default.aspx?wa=wsignin1.0"&gt;Dr. James McCaffrey&lt;/a&gt;. He said that LINQ reduced the time it took him to write test procedures by 50 percent. He added that LINQ to SQL made his test code &amp;quot;much shorter, much cleaner, and therefore easier to create, modify, and maintain.&amp;quot;&lt;/p&gt;  &lt;p&gt;Of course, LINQ has many other benefits. It provides a single unified query language that can be used across multiple data sources including SQL and XML. It is integrated into the C# language, allowing you to harness the power of .NET and the Visual Studio IDE when writing queries. It is both transformative and composable, allowing you to combine queries from multiple data sources in myriad ways. It uses a succinct and elegant declarative style of programming, and it is extensible so that you can run LINQ queries against any arbitrary data source. Yet sometimes it is simplest just to think of LINQ as a fast, elegant way to get a lot of work done quickly. In this day and age when we are all struggling to work with huge amounts of data, and to complete complex tasks in a short period of time, anything that will simplify our lives is welcome. LINQ is one of those things that just makes it easier to get work done quickly.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjamesmccaffrey.spaces.live.com%2fdefault.aspx%3fwa%3dwsignin1.0"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjamesmccaffrey.spaces.live.com%2fdefault.aspx%3fwa%3dwsignin1.0" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8607571" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/charlie/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LINQ/default.aspx">LINQ</category></item></channel></rss>