<?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 : LinqFarm</title><link>http://blogs.msdn.com/charlie/archive/tags/LinqFarm/default.aspx</link><description>Tags: LinqFarm</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>HDI Video: Generate from Usage in Visual Studio 2010 with Karen Liu</title><link>http://blogs.msdn.com/charlie/archive/2009/11/07/hdi-video-generate-from-usage-with-karen-liu.aspx</link><pubDate>Sat, 07 Nov 2009 23:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9898211</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/charlie/comments/9898211.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=9898211</wfw:commentRss><description>&lt;style type="text/css"&gt;




span.squiggle { color: red; text-decoration: underline;}
.listing { font-weight: bold }
.figure { font-weight: bold }
.note { font-weight: italic; background: lightgray; border: 1px solid black; }&lt;/style&gt;  &lt;p&gt;Karen Liu, the Lead Program Manager for the Visual C# and Visual Basic IDEs, has created a &lt;a href="http://msdn.microsoft.com/en-us/vcsharp/ee633445.aspx"&gt;new video&lt;/a&gt; on &lt;a href="http://msdn.microsoft.com/en-us/library/dd409796(VS.100).aspx"&gt;Generate from Usage&lt;/a&gt; (GFU), a feature found in Visual Studio 2010, Beta 2. This post recaps what is included in the video, including the sections that show how GFU can be used to enhance &lt;a href="http://www.extremeprogramming.org/rules/testfirst.html"&gt;test first development&lt;/a&gt;. The video is shot in both VB and C#. Since this is a C# blog, I’ll show only C# code, and will translate the VB code that Karen shows into C#.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=8077"&gt;source code&lt;/a&gt; from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Generate from Usage (GFU) is a new feature in 2010 that allows you to write code that consumes a library or API before that API even exists. Using Visual Studio menus or shortcuts, you can automatically generate classes, constructors, methods, fields and properties from the code you typed in the editor.&lt;/p&gt;  &lt;p&gt;As the video begins, Karen first types in the code to initialize a class called &lt;strong&gt;Automobile&lt;/strong&gt; that does not yet exist:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5cf3a3f0-6bee-4a46-8d17-8a5e29158786" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; myCar = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; Automobile(Make: &lt;span style="color:#a31515"&gt;&amp;quot;Honda&amp;quot;&lt;/span&gt;, Model: &lt;span style="color:#a31515"&gt;&amp;quot;Accord&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Notice that the class name &lt;strong&gt;Automobile&lt;/strong&gt; is &lt;u&gt;not&lt;/u&gt; colored in teal, which is Visual Studio’s way of telling you that it does not yet it exist. When seen inside Visual Studio, the word &lt;strong&gt;Automobile&lt;/strong&gt; will also have red squiggly (or wavy) underline, and a small blue&lt;strong&gt; smart tag &lt;/strong&gt;under the letter A, as shown in Figure 1. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure01_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure01" border="0" alt="Figure01" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure01_thumb.png" width="644" height="161" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p class="figure"&gt;Figure 1: The red squiggle is Visual Studio’s way of telling you that the type &lt;u&gt;Automobile&lt;/u&gt; will not compile. The blue &lt;em&gt;smart tag&lt;/em&gt; under the letter A let’s the user know that an expansion tip is available by simultaneously pressing the Control key and a period (Ctrl + .).&lt;/p&gt;  &lt;p&gt;The smart tag under the letter A is Visual Studio’s way of telling you to press the control and period keys to bring up a special expansion tip, as shown in Figure 2. Here we see two options, one for directly creating a class called &lt;strong&gt;Automobile&lt;/strong&gt;, and the other for bringing up a dialog which allows us to define or tweak the details of the type of we create.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure02_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure02" border="0" alt="Figure02" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure02_thumb.png" width="644" height="247" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p class="figure"&gt;Figure 2: Viewing an expansion tip in Visual Studio.&lt;/p&gt;  &lt;p&gt;You can also bring up the options to create a new class or type by right clicking on the word &lt;strong&gt;Automobile&lt;/strong&gt; and selecting &lt;strong&gt;Generate&lt;/strong&gt; from the popup menu, as shown in Figure 3.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure03_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure03" border="0" alt="Figure03" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure03_thumb.png" width="644" height="174" /&gt;&lt;/a&gt; &lt;/strong&gt;&lt;/p&gt;  &lt;p class="figure"&gt;Figure 3: A second way to generate a class is to right click the word &lt;u&gt;Automobile&lt;/u&gt; and choose &lt;u&gt;Generate &lt;/u&gt;from the popup menu.&lt;/p&gt;  &lt;p&gt;If you select the option to create a new class, then Visual Studio will automatically generate a new file called &lt;strong&gt;Automobile.cs&lt;/strong&gt; and place inside it a new class called &lt;strong&gt;Automobile&lt;/strong&gt;. The entire generated file is shown in Listing 1.&lt;/p&gt;  &lt;p class="listing"&gt;Listing 1: The code generate when you choose to create a new class based on an undefined identifier in Visual Studio 2010.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:51302077-3aa5-4dcb-94f7-45db8bcc830f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Linq;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Text;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; DeleteMe&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Automobile&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;You can now go back to your original source file where you will see that there is still a red squiggly and blue tool tip, as shown in Figure 4. These hints are shown because we have not yet created a constructor for the &lt;strong&gt;Automobile&lt;/strong&gt; class. If we again press control plus period, then the option to generate these code elements is made available to us, as shown in Figure 4.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure04_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure04" border="0" alt="Figure04" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure04_thumb_1.png" width="644" height="146" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p class="figure"&gt;Figure 4: Selecting the smart tag a second time brings up an option to automatically generate the constructor and associated fields for your class.&lt;/p&gt;  &lt;p&gt;The code that is generated by selecting the expansion tip is shown in Listing 2. Notice that on lines 10 and 11 fields were created for your class and on lines 16 and 17 code was generated inside the constructor for initializing them. A comment in the form of a &lt;strong&gt;TODO&lt;/strong&gt; item is also added to your class. These &lt;strong&gt;TODO&lt;/strong&gt; items are visible in the Visual Studio &lt;a href="http://msdn.microsoft.com/en-us/library/zce12xx2(VS.100).aspx"&gt;Task List&lt;/a&gt;. You can access the &lt;strong&gt;Task List &lt;/strong&gt;by choosing &lt;strong&gt;View | Task List &lt;/strong&gt;from the menu, or by pressing &lt;strong&gt;Ctrl+W, T&lt;/strong&gt;. Be sure to choose the &lt;strong&gt;Comments&lt;/strong&gt; option from the drop down at the top of the &lt;strong&gt;Task List&lt;/strong&gt;.&lt;/p&gt;  &lt;p class="listing"&gt;Listing 2: The code for the Automobile’s constructor is automatically generated by the IDE.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:39825868-2600-44c3-aca2-c3e2393bc188" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Linq;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Text;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; GenerateFromUsageSample&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Automobile&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Make;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Model;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Automobile(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Make, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Model)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#008000"&gt;// TODO: Complete member initialization&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Make = Make;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Model = Model;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Karen next types in code for a &lt;strong&gt;TurnLeft &lt;/strong&gt;method, and for a property called &lt;strong&gt;IsFacingNorth&lt;/strong&gt;. As she types each item, a smart tag appears, and again she has the option to press &lt;strong&gt;Ctrl+.&lt;/strong&gt; to automatically generate code for the &lt;strong&gt;Automobile &lt;/strong&gt;class. The code typed in the program’s entry point is shown in Listing 3, and the code generated by the IDE is shown mostly in Listing 4. Notice, however, that code for a private object called &lt;strong&gt;distance &lt;/strong&gt;was automatically generated in the &lt;strong&gt;Main&lt;/strong&gt; method. The IDE actually gives you the option to create either a &lt;strong&gt;private &lt;/strong&gt;field, as shown here, or a &lt;strong&gt;public&lt;/strong&gt; property.&lt;/p&gt;  &lt;p class="listing"&gt;Listing 3: The complete code for Karen’s first sample includes a constructor, a method called &lt;u&gt;TurnLeft&lt;/u&gt;, and a property called &lt;u&gt;IsFacingNorth&lt;/u&gt;.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:98dfe693-a81d-4df2-8435-00a22ea59678" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; GenerateFromUsage&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Program&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt; distance;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[] args)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;var&lt;/span&gt; myCar = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Automobile&lt;/span&gt;(Make: &lt;span style="color:#a31515"&gt;&amp;quot;Honda&amp;quot;&lt;/span&gt;, Model: &lt;span style="color:#a31515"&gt;&amp;quot;Accord&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;myCar.TurnLeft(distance);&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;myCar.IsFacingNorth = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p class="listing"&gt;Listing 4: The code generated by the IDE for the TurnLeft method and the IsFacingNorth property.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f7d9d6bd-a681-4139-b84f-0468606a8287" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; GenerateFromUsage&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Automobile&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Make;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Model;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; Automobile(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Make, &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Model)&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#008000"&gt;// TODO: Complete member initialization&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Make = Make;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.Model = Model;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;internal&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; TurnLeft(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; distance)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;NotImplementedException&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;bool&lt;/span&gt; IsFacingNorth { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;When looking at Listing 4, notice that code for generating a &lt;strong&gt;NotImplementedException &lt;/strong&gt;is automatically generated in the &lt;strong&gt;TurnLeft &lt;/strong&gt;method. The IDE was also smart enough to discern that the &lt;strong&gt;IsFacingNorth&lt;/strong&gt; property returns a &lt;strong&gt;bool&lt;/strong&gt;. All the lines of code shown in Listing 4 were generated by typing just three lines of code in the &lt;strong&gt;main &lt;/strong&gt;method found in Listing 3.&lt;/p&gt;  &lt;h2&gt;The C# Side&lt;/h2&gt;  &lt;p&gt;In the second part of the video Karen shows how you can use &lt;em&gt;generate from usage&lt;/em&gt; (GFU) to enhance your experience when creating unit tests. Many theorists advocate using a test first methodology in which you first create a test, and then write the code that you want to test. You will see that GFU can be used to make this style of development quite natural and simple to use inside of Visual Studio.&lt;/p&gt;  &lt;p class="note"&gt;NOTE: This section of the post requires high end versions of Visual Studio that contain the test wizards. Even if you don’t have those tools, you can still follow along to see how Generate from Usage can be used in all versions of Visual Studio to make it easier to create unit tests.&lt;/p&gt;  &lt;p&gt;Begin by starting a new console application called &lt;strong&gt;Customer&lt;/strong&gt;. Choose &lt;strong&gt;Tests | New Test &lt;/strong&gt;from the Visual Studio menu. Select &lt;strong&gt;Basic Unit Test&lt;/strong&gt; from the dialog, and name it &lt;strong&gt;Customers&lt;/strong&gt;, as shown in Figure 5. When you press &lt;strong&gt;OK &lt;/strong&gt;in the &lt;strong&gt;Add New Test &lt;/strong&gt;dialog you will be prompted for the name of your unit test project. Type in &lt;strong&gt;CustomerTests&lt;/strong&gt;. When you are done, the &lt;strong&gt;Solution Explorer&lt;/strong&gt; for your project should look as it does in Figure 6.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure06_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure06" border="0" alt="Figure06" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure06_thumb.png" width="577" height="484" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p class="figure"&gt;Figure 5: Create a new unit test project that will house a file called Customers.&lt;/p&gt;  &lt;p&gt;Right click on the &lt;strong&gt;Customer&lt;/strong&gt; node in the &lt;strong&gt;solution explorer&lt;/strong&gt; and choose &lt;strong&gt;Add | New Folder&lt;/strong&gt; to create a new directory called &lt;strong&gt;Models&lt;/strong&gt;. When you are done you should see a new node in the &lt;strong&gt;solution explorer&lt;/strong&gt;, as shown in Figure 6. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure07_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure07" border="0" alt="Figure07" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure07_thumb.png" width="315" height="386" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;p class="figure"&gt;Figure 6: This solution contains two projects, one a simple console application called &lt;u&gt;Customer &lt;/u&gt;that contains a folder called &lt;u&gt;Models&lt;/u&gt;. The other project is called &lt;u&gt;CustomerTests&lt;/u&gt; and it is designed to hold unit tests.&lt;/p&gt;  &lt;p&gt;I want to check if my Customer list is create correctly. In the Customers file from your test project, create a method called IsCustomerListValid. Inside the method create code to initialize a &lt;strong&gt;CustomerList&lt;/strong&gt; object, as shown in Listing X. &lt;/p&gt;  &lt;p&gt;Listing 5: A simple unit test with the code to initialize a class called CustomerList. Note that the CustomerList is not colored in Teal, since its declaration has not been created yet.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fbbd71a3-4443-4c5b-bdba-8d3dee069dfa" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Text;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Linq;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; Microsoft.VisualStudio.TestTools.UnitTesting;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; CustomerTests&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af"&gt;TestClass&lt;/span&gt;]&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Customers&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; IsCustomerListValid()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;CustomerList cust = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; CustomerList();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;At this stage, there is no CustomerList, so in Visual Studio you will again see the red squiggles and the blue smart tag, just as I showed in Figure 2. Without needing to take your hands off the keyboard, you can press control plus dot to bring up the expansion tips as in Figure 2. This time select &lt;strong&gt;New Type. &lt;/strong&gt;A dialog comes up like the one shown in Figure 7. In the dialog we can change the accessibility, the kind of code to generate and the project file and directory where we want to place it. Set the &lt;strong&gt;Access&lt;/strong&gt; to &lt;strong&gt;public&lt;/strong&gt; and the &lt;strong&gt;Kind&lt;/strong&gt; to &lt;strong&gt;class&lt;/strong&gt;. Use the &lt;strong&gt;Project&lt;/strong&gt; drop down to select the &lt;strong&gt;Customer&lt;/strong&gt; project and the the &lt;strong&gt;Create new file&lt;/strong&gt; drop down to select the &lt;strong&gt;Model&lt;/strong&gt; directory. Type in &lt;strong&gt;CustomerList.cs &lt;/strong&gt;as the name of the file to create. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure05_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Figure05" border="0" alt="Figure05" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/HDIVideoGeneratefromUsagewithKarenLiu_7209/Figure05_thumb.png" width="378" height="407" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p class="figure"&gt;Figure 7. Selecting the kind of type that you want to create, as well as the project, folder and file in which you want to place it.&lt;/p&gt;  &lt;p&gt;When you press OK in the&lt;strong&gt; Generate New Type&lt;/strong&gt; dialog, the new type will be created, and the IDE will add the using directive and the correct references. You can now fill out your test and your generated class as shown in Listing 6 and 7. Note that we have used generate from usage to enhance the &lt;strong&gt;CustomerList&lt;/strong&gt; class with a method called &lt;strong&gt;Add.&lt;/strong&gt; I then manually wrote code to create a list into which the text we pass can be inserted.&lt;/p&gt;  &lt;p class="listing"&gt;Listing 6: A simple unit test.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2d4608a7-da3a-480f-8b2f-520f6b9aea55" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; Customer.Models;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; Microsoft.VisualStudio.TestTools.UnitTesting;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; CustomerTests&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af"&gt;TestClass&lt;/span&gt;]&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Customers&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;[&lt;span style="color:#2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; IsCustomerListValid()&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#2b91af"&gt;CustomerList&lt;/span&gt; cust = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;CustomerList&lt;/span&gt;();&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;cust.Add(&lt;span style="color:#a31515"&gt;&amp;quot;Karen&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#2b91af"&gt;Assert&lt;/span&gt;.IsNotNull(cust);&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p class="listing"&gt;Listing 7: The using directive, namespace, CustomerList class and header for the Add method were auto-generated. I manually created the list and inserted the call to Add an item into it.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2c11c353-a69d-4687-bb78-e50d45f80650" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; Customer.Models&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;CustomerList&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;&amp;gt; list = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Add(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; p)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;{&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;list.Add(p);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;You can now use the Test | View menu to bring up the Test List Editor and Test View to select and run your test.&lt;/p&gt;  &lt;h2&gt;Summary&lt;/h2&gt;  &lt;p&gt;In this post you got a second look at the code used in Karen Liu’s generate from usage video. You saw that GFU can be used to create classes, constructors, parameters, properties and fields. You also explored the powerful &lt;strong&gt;Generate New Type&lt;/strong&gt; dialog which gives you the flexibility to choose the kind of type you want to create, as well as the project, directory and file in which you want to insert it. Finally, you saw that generate from usage can be powerful aid when you are engaged in test first development.&lt;/p&gt;  &lt;h2&gt;Resources&lt;/h2&gt;  &lt;ul&gt;   &lt;li&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=8077"&gt;source code&lt;/a&gt; from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/vcsharp/ee633445.aspx"&gt;The How Do I Generate from Usage Video&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd409796(VS.100).aspx"&gt;Generate from Usage in the MSDN Library&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/vbteam/archive/2008/12/13/walkthrough-tdd-support-with-the-generate-from-usage-feature-in-vs-2010-lisa-feigenbaum.aspx"&gt;Walkthrough: TDD Support with the Generate From Usage Feature in VS 2010 (Lisa Feigenbaum)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blog.noop.se/archive/2009/06/16/generate-from-usage-in-visual-studio-2010.aspx"&gt;Magnus Martensson on Generate from Usage&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/zce12xx2(VS.100).aspx"&gt;Task Lists and TODO comments in the MSDN Library&lt;/a&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%2f11%2f07%2fhdi-video-generate-from-usage-with-karen-liu.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%2f11%2f07%2fhdi-video-generate-from-usage-with-karen-liu.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9898211" 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/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://blogs.msdn.com/charlie/archive/tags/Video/default.aspx">Video</category><category domain="http://blogs.msdn.com/charlie/archive/tags/LinqFarm/default.aspx">LinqFarm</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/Testing/default.aspx">Testing</category></item><item><title>Silverlight Revisited</title><link>http://blogs.msdn.com/charlie/archive/2009/04/02/silverlight-revisited.aspx</link><pubDate>Fri, 03 Apr 2009 04:56:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9530039</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/charlie/comments/9530039.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=9530039</wfw:commentRss><description>&lt;p&gt;I’ve written about Silverlight in previous posts published on this blog. Recently, however, I’ve found my thoughts coming back to this subject. This is hardly unusual: many people are interested in Silverlight.&lt;/p&gt;  &lt;p&gt;The reason for the attraction to this technology is not hard to discover. Silverlight allows us to build visually appealing web applications with a sophisticated, well designed programming language such as C#. Though there are now many sophisticated tools for creating web applications, but most of them still depend on fragmented models that force us to integrate multiple, hard to use technologies into a patchwork design that is poorly integrated. Applications built on these models can be hard to debug, and hard to maintain. They also lack the ability to easily support rich interfaces that would be taken for granted in a desktop application.&lt;/p&gt;  &lt;p&gt;I’ve spent most of my programming career working in either Delphi or C#, and I have a huge respect for the elegance of these languages, and in the power of object-oriented design to help us break complex problems into manageable, easy to maintain solutions. It is, of course, easy to create poorly designed C# programs. Nevertheless, I believe that C# in particular and Silverlight in general has the capacity, the potential, to support elegant solutions in a way that many web technologies do not.&lt;/p&gt;  &lt;p&gt;In short, I’m interested in Silverlight for two reasons:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;It allows me to create a rich interface with sophisticated visual techniques including animation. &lt;/li&gt;    &lt;li&gt;It provides a thoroughly modern programming model, and a set of rich API’s for performing common programming tasks. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;So there, whether one agrees with me or not, is my motivation to revisit Silverlight. Once I had found my inspiration, my next step was to visit the &lt;a href="http://silverlight.net/default.aspx"&gt;Silverlight site&lt;/a&gt;, and &lt;a href="http://silverlight.net/GetStarted/"&gt;download&lt;/a&gt; the tools to integrate Silverlight into Visual Studio 2008. The installation worked smoothly on a copy of XP that I had running in a virtual machine. Though I haven’t done it yet, I also intend to download Expression Blend.&lt;/p&gt;  &lt;p&gt;Once I had a set of Silverlight tools integrated into Visual Studio, I set about experimenting with them. After a bit, I came up with the application shown in Figure 1. Not bound by such quotidian obligations as having a practical purpose, or a pleasing interface, it didn’t take me long to put this program together. It’s only redeeming feature was that it gave me a chance to experiment with several Silverlight technologies including:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;XAML and WPF &lt;/li&gt;    &lt;li&gt;A tool called a StackPanel, which helped me organize the interface, which consists primarily of simple controls such as buttons and text areas. &lt;/li&gt;    &lt;li&gt;Two different types of Canvases on which I could I draw.&amp;#160; &lt;/li&gt;    &lt;li&gt;Several different graphical shapes and paths, as well as the Ink technology handed down from Tablet PC. &lt;/li&gt;    &lt;li&gt;A simple LINQ query. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightRevisited_96E3/Silverlight01.png"&gt;&lt;img title="Silverlight01" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="396" alt="Silverlight01" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/SilverlightRevisited_96E3/Silverlight01_thumb.png" width="324" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Figure 1: A simple Silverlight program, running in the Internet Explorer, which demonstrates how to use several key technologies.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In my next post, I’ll talk some about the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=5305"&gt;code&lt;/a&gt; for this application, and describe how its various parts fit together. Overall, I found putting this code together was blessedly simple, and overall quite a bit of fun. At least from what I’ve seen so far, one of Silverlight’s great strengths is its ease of use.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Download the &lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=5305"&gt;SilverlightDrawingTest&lt;/a&gt; from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&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%2f04%2f02%2fsilverlight-revisited.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%2f2009%2f04%2f02%2fsilverlight-revisited.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9530039" 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/LinqFarm/default.aspx">LinqFarm</category><category domain="http://blogs.msdn.com/charlie/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Query Data with Parallel LINQ</title><link>http://blogs.msdn.com/charlie/archive/2008/12/15/query-data-with-parallel-linq.aspx</link><pubDate>Tue, 16 Dec 2008 09:00:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9224144</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>23</slash:comments><comments>http://blogs.msdn.com/charlie/comments/9224144.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=9224144</wfw:commentRss><description>&lt;p&gt;This post shows a simple way to write code that takes advantage of multiple processors. You will see that LINQ queries can allow you to side step the difficult tasks normally involved in writing multi-threaded code. To get started, all you need is a little basic knowledge of how to write simple LINQ queries. &lt;p&gt;The code shown in this post uses a pre-release version of PLINQ called the Microsoft Parallel Extensions to .NET Framework 3.5. When PLINQ finally ships, it will run only on .NET 4.0 or later. The version I'm using that runs on top of 3.5 is for evaluation purposes only. There will never be a shipping version that runs on .NET 3.5.  &lt;p&gt;This LINQ provider is being created at Microsoft by the Parallel Computing team; it is not the work of the C# team that created LINQ to Objects and LINQ to SQL. Here is the website for the Parallel Computing team:  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/concurrency/"&gt;http://msdn.microsoft.com/en-us/concurrency/&lt;/a&gt;  &lt;p&gt;At the time of this writing, these extensions were available only in pre-release form. You could download them either as Visual Studio 2008 compatible extensions to .NET 3.5, or as part of the pre-release version of Visual Studio 2010. Since the download sites might change over the coming months, I suggest that you find these resources by going to the Parallel Computing site, or to the Visual Studio site:  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/vs2008"&gt;http://msdn.microsoft.com/en-us/vs2008&lt;/a&gt;  &lt;p&gt;Parallel LINQ, or PLINQ, is only a small part of the Parallel Extensions to the .NET Framework. It is, however, an important part. Since it is a simple and natural extension of the LINQ syntax, I think developers familiar with that technology will find it easy to use.  &lt;p&gt;Consider this code:&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; list = &lt;span style="color: rgb(43,145,175)"&gt;Enumerable&lt;/span&gt;.Range(1, 10000);

&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; q = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; x &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; list.AsParallel()
        &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; x &amp;lt; 3300
        &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; x;

&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; x &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; q)
{
    &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(x);
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;These lines look nearly identical to the code you have seen in many simple LINQ samples. The only significant difference is the call to &lt;strong&gt;AsParallel&lt;/strong&gt; at the end of the first line. Though we have often used type inference to hide the return type of a LINQ query, I'm going to pause and take a second look at this instance. Rather than returning IEnumerable&amp;lt;T&amp;gt;, this version of PLINQ returns &lt;strong&gt;IParallelEnumerable&lt;/strong&gt;&amp;lt;&lt;strong&gt;int&lt;/strong&gt;&amp;gt;:&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;IParallelEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; q = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; x &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; list.AsParallel() etc….&lt;/pre&gt;
&lt;p&gt;In the near future, PLINQ queries of this type will probably return &lt;strong&gt;ParallelQuery&lt;/strong&gt;&amp;lt;&lt;strong&gt;int&lt;/strong&gt;&amp;gt;. Because this product is still evolving, it might be simplest to use var, at least during the pre-release phase, and let the compiler choose the type. That way you can save typing, avoid problems with anonymous types, and you need not concern yourself about changes in the API as the product develops. It is almost always appropriate to use &lt;strong&gt;var&lt;/strong&gt; to designate the return type of a LINQ query, and there are only special circumstances when you would do otherwise. 
&lt;p&gt;Here are the results from this first PLINQ query: 
&lt;div&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;2
1
3
4
6
512
5
7
513
8
12
514
9
13
515
10
14
516
11
15
517
16
72
518
17&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The numbers shown here are in a relatively random order because they are being returned from different threads. It is important to remember that the sequence of values returned by LINQ is not always guaranteed to be presented in a particular order. If Order is important in your code, you can add a call to &lt;strong&gt;AsOrdered&lt;/strong&gt; to the query after the call to &lt;strong&gt;AsParallel&lt;/strong&gt;. Alternatively, you could insert a &lt;strong&gt;GroupBy&lt;/strong&gt; clause to establish the desired ordering. Otherwise developers should assume that the ordering from a PLINQ query will be entirely random 
&lt;p&gt;Now that you understand the basics of Parallel LINQ, let’s move on to look at a more interesting example. Improved performance is the main reason to write code that can run in parallel. The program shown in this post uses a timer to demonstrate how PLINQ can improve performance in a program. 
&lt;p&gt;Performance improvements become more evident when our code has access to more processors. The code I show here runs faster on a two processor machine, but it really starts to come into its own on a four processor machine. Moving up to even more processors yields more powerful results. Here, for instance, are the results showing an improvement of 1.33 times when using two processors, and almost two times when using 4 processors: 
&lt;div&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;2 Processors = 1.44 x improvement:
Linear: 00:00:13.15
Parallels: 00:00:09.10

4 Processors = 1.96 x improvement:
Linear: 00:00:15.00
Parallel: 00:00:07.68&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These tests are being running against pre-release software, so these numbers are almost certain to change before release, and of course different machines will yield different results. Furthermore, the degree of improvement that you see is likely to change depending on the type of algorithm you run, the number of cores on your machine, the architecture of the machine, how many caches there are and how they’re laid out, etc. Though it is rare, some queries show superlinear performance enhancements. In other words, there is a greater than 4x speedup on a 4-core box. An improvement of 2 times, such as the one shown, or even a 3 time improvement, is common. 
&lt;p&gt;This sample program is called FakeWeatherData, and it is available for download from 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;. It features a simple LINQ to XML query run against a file with 10,000 records in it. The data I'm querying is not real, but consists of random dates and temperatures generated by a simple algorithm included in the &lt;strong&gt;FakeWeatherData&lt;/strong&gt; program. 
&lt;p&gt;The XML file is structured like this:&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;"&lt;span style="color: rgb(0,0,255)"&gt;1.0&lt;/span&gt;"&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;"&lt;span style="color: rgb(0,0,255)"&gt;utf-8&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;Samples&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;Sample&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;Year&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;1973&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Year&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;Month&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;May&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Month&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;Day&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;15&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Day&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;Temperature&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;10&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Temperature&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;Sample&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;Sample&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;Year&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;1970&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Year&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;Month&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;Feb&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Month&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;Day&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;10&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Day&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;Temperature&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;14&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Temperature&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;Sample&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;Sample&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;Year&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;1970&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Year&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;Month&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;Jan&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Month&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;Day&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;15&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Day&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;Temperature&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;11&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Temperature&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;Sample&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;  ... Many lines of code omitted here&lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163,21,21)"&gt;Samples&lt;/span&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;gt;
&lt;/span&gt;&lt;font color="#0000ff"&gt;&lt;/font&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;There is also a simple C# class used by the program to encapsulate the data from the XML file: &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;WeatherData
&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;string&lt;/span&gt; Year { &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(0,0,255)"&gt;set&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;string&lt;/span&gt; Month { &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(0,0,255)"&gt;set&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;string&lt;/span&gt; Day { &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(0,0,255)"&gt;set&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;string&lt;/span&gt; Temperature { &lt;span style="color: rgb(0,0,255)"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(0,0,255)"&gt;set&lt;/span&gt;; }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The parallel version of the query in the program looks like this: &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; i = 0; i &amp;lt; NUM_REPS; i++)
{
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; list = (&lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; x &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; doc.Root.Elements(&lt;span style="color: rgb(163,21,21)"&gt;"Sample"&lt;/span&gt;).AsParallel()
                &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Year"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"1973"&lt;/span&gt; &amp;amp;&amp;amp;
                   x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Month"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"Apr"&lt;/span&gt; &amp;amp;&amp;amp;
                   x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Day"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"15"
&lt;/span&gt;                &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;WeatherData
&lt;/span&gt;                {
                    Day = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Day"&lt;/span&gt;).Value,
                    Month = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Month"&lt;/span&gt;).Value,
                    Temperature = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Temperature"&lt;/span&gt;).Value,
                    Year = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Year"&lt;/span&gt;).Value
                }).ToList();

}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Accompanying this code is a similar LINQ query that does not use PLINQ &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; i = 0; i &amp;lt; NUM_REPS; i++)
{
    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; list = (&lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; x &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; doc.Root.Elements(&lt;span style="color: rgb(163,21,21)"&gt;"Sample"&lt;/span&gt;)
                &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Year"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"1973"&lt;/span&gt; &amp;amp;&amp;amp; 
                   x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Month"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"Apr"&lt;/span&gt; &amp;amp;&amp;amp; 
                   x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Day"&lt;/span&gt;).Value == &lt;span style="color: rgb(163,21,21)"&gt;"15"
&lt;/span&gt;                &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;WeatherData
&lt;/span&gt;                {
                    Day = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Day"&lt;/span&gt;).Value,
                    Month = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Month"&lt;/span&gt;).Value,
                    Temperature = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Temperature"&lt;/span&gt;).Value,
                    Year = x.Element(&lt;span style="color: rgb(163,21,21)"&gt;"Year"&lt;/span&gt;).Value
                }).ToList();

}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The program queries the data in the XML file first using the Parallel code, then using standard LINQ. By comparing the time it takes each block of code to execute you can get a sense of the relative improvement available through PLINQ. I'll show you how to make such comparisons in just a moment. I will also discuss some tools that will become available to help profile code of this type. 
&lt;p&gt;You can see that the PLINQ query contains a call to &lt;strong&gt;AsParallel&lt;/strong&gt;, while the other query does not. Other than that the two queries are identical. The fact that the two queries look so much alike points to a primary strength of PLINQ: very little specialized knowledge is necessary in order to begin using it. This does not mean that the subject is trivial, but only that the barrier to entry is low. This is not the case with most concurrent programming models. 
&lt;p&gt;LINQ queries are designed to be read-only, working with immutable data. This is a good model for parallelism, because it makes it unlikely that data will mutate, thereby setting up the potential for a race condition. You should note, however, that PLINQ does nothing to prevent this from happening, it is simply that LINQ is designed to make it unlikely. 
&lt;p&gt;Note also that the declarative LINQ programming style ensures that developers specify what they want done, rather than how it should be done. This leaves PLINQ free to ensure that concurrent LINQ queries run in the safest manner possible. If LINQ had been defined more strictly, such that it had to process each element in a certain order, then the PLINQ team would have had a much more difficult task. 
&lt;p&gt;The code in both these queries pulls out only the records from the XML file that have their date set to April 15, 1973. Because of deferred execution, the query would not do anything if I did not call ToList(). As a result, I added that call and converted the result into a List&amp;lt;WeatherData&amp;gt;. Though hardly earthshaking in import, these calls ensure that the code actually does something, and thus gives PLINQ scope to take advantage of the multiple processers on your system. 
&lt;p&gt;Simple timers are created to measure the difference between the standard LINQ query and the PLINQ query. I've also used a method used in many of Parallel LINQ team's samples for displaying the time elapsed during a test run: &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; RunTest()
{
    &lt;span style="color: rgb(43,145,175)"&gt;XDocument&lt;/span&gt; doc = &lt;span style="color: rgb(43,145,175)"&gt;XDocument&lt;/span&gt;.Load(&lt;span style="color: rgb(163,21,21)"&gt;"XMLFile1.xml"&lt;/span&gt;);

    &lt;span style="color: rgb(43,145,175)"&gt;Stopwatch&lt;/span&gt; sw = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Stopwatch&lt;/span&gt;();

    sw.Start();
    LinqOrdinarie(doc);
    sw.Stop();
    ShowElapsedTime(&lt;span style="color: rgb(163,21,21)"&gt;"Ordinaire"&lt;/span&gt;, sw.Elapsed);

    sw.Reset();

    sw.Start();
    ParallelLinq(doc);
    sw.Stop();
    ShowElapsedTime(&lt;span style="color: rgb(163,21,21)"&gt;"Parallels"&lt;/span&gt;, sw.Elapsed);
}
&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0,128,0)"&gt;&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(43,145,175)"&gt;TimeSpan&lt;/span&gt; ShowElapsedTime(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; caption, &lt;span style="color: rgb(43,145,175)"&gt;TimeSpan&lt;/span&gt; ts)
{
    &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; elapsedTime = &lt;span style="color: rgb(43,145,175)"&gt;String&lt;/span&gt;.Format(&lt;span style="color: rgb(163,21,21)"&gt;"{0}: {1:00}:{2:00}:{3:00}.{4:00}"&lt;/span&gt;,
        caption, ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);
    &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(elapsedTime, &lt;span style="color: rgb(163,21,21)"&gt;"RunTime"&lt;/span&gt;);
    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; ts;
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;At least with the pre-release version of PLINQ that I've played with, I've found it very useful to set up timers to confirm that PLINQ is actually able to speed up an operation. My record at guessing which code will benefit from running in parallel is not good, and so I find that confirming the effectiveness of the code by explicitly measuring it is worthwhile. You can either use the simple StopWatch class from the System.Diagnostics namespace, as shown here, or else you can use a profiler. Note that a thread aware profiler might ship with some versions of Visual Studio 2010. 
&lt;p&gt;I've found that the advantages of concurrent LINQ become more obvious the longer the operation I'm timing lasts. As a result, I've placed the query inside a loop, and added a variable to the program called NUM_REPS. By setting NUM_REPS to a large number, say 500, you can clearly see the benefits that can be accrued when you run LINQ queries in parallel on multiple processors. Note that the first time PLINQ is used, its assembly will need to be loaded, the relevant types will need to be JIT compiled, and new threads will need to be spun up, etc. As a result, many developers see improved performance after they get past the initial warm-up time. 
&lt;p&gt;Though it is very easy to get started with PLINQ, there are still complexities inherent in the subject that you need to consider. For instance, PLINQ will sometimes develop a different partitioning scheme for your data depending on whether you are working with an Enumerable or an Array. To learn more about this subject, see the following post from the Parallel Programming team: 
&lt;p&gt;&lt;a href="http://blogs.msdn.com/pfxteam/archive/2007/12/02/6558579.aspx"&gt;http://blogs.msdn.com/pfxteam/archive/2007/12/02/6558579.aspx&lt;/a&gt; 
&lt;p&gt;The simple PLINQ examples shown in this post should help you get started with this powerful and interesting technology. Parallel LINQ is still in its infancy, but already it provides means of greatly simplifying tasks that are not normally easy to perform.&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f12%2f15%2fquery-data-with-parallel-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%2f2008%2f12%2f15%2fquery-data-with-parallel-linq.aspx"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9224144" 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/LinqFarm/default.aspx">LinqFarm</category><category domain="http://blogs.msdn.com/charlie/archive/tags/Parallel/default.aspx">Parallel</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>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>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>Charlie Speaking in Southern California</title><link>http://blogs.msdn.com/charlie/archive/2008/04/29/charlie-speaking-in-southern-california.aspx</link><pubDate>Wed, 30 Apr 2008 09:36:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8441737</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8441737.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8441737</wfw:commentRss><description>&lt;p&gt;I will be presenting on C# and LINQ three times in Southern California next week at a series of user group meetings. The events will be held in the &lt;a href="http://sddotnetdg.org/"&gt;San Diego&lt;/a&gt; and the &lt;a href="http://www.socalnetevents.org/"&gt;Los Angeles area&lt;/a&gt;. &lt;a href="http://blogs.msdn.com/vbteam/archive/tags/Lisa+Feigenbaum/default.aspx"&gt;Lisa Feigenbaum&lt;/a&gt; will be flying down from Redmond with me, and she will do VB presentations at the same set of meetings. &lt;/p&gt;  &lt;p&gt;The schedule is as follows:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;May 6, 6:00 PM: San Diego, &lt;a href="http://sddotnetdg.org/"&gt;http://sddotnetdg.org/&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;May 7, 6:30 PM: Irvine, &lt;a href="http://www.ocdotnet.org/"&gt;http://www.ocdotnet.org/&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;May 8: 6:30 PM: Torrence, &lt;a href="http://www.southbaynet.org/southbaynet.org/default.html"&gt;http://www.southbaynet.org/southbaynet.org/default.html&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I will be talking on &lt;strong&gt;C# 3.0 Best Practices and LINQ&lt;/strong&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;C# 3.0 introduces a number of new language features such as query expressions, lambda expressions, extension methods, automatically implemented properties, local type inference and more. These are all features that can improve the quality of your code. They also provide new opportunities for making mistakes This talk focuses on both the good and the bad: how to use and how not to use the new features of C#. Each feature will be introduced with a small example, and you should be able to follow the talk even if you are not already familiar with the new language constructs. The talk will also explore the theoretical underpinnings of LINQ.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Lisa will be speaking on &lt;b&gt;Visual Basic 2008 IDE Tips and Tricks:&lt;/b&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;In this talk, we&amp;#8217;ll show how to turn yourself into a Visual Studio 2008 guru with the new language and IDE features. Tips and tricks will include how to maximize your VB IntelliSense experience, leverage Refactoring features, and improve the performance of your query and XML code. We&amp;#8217;ll explore integrated XML, and show how to navigate XML gotchas and express what you wish in fewer lines of code. With respect to LINQ we&amp;#8217;ll go deep into best practices, pitfalls to avoid, and answers to most frequently asked questions&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Tuesday Night: &lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.amnhealthcare.com/"&gt;AMN Health Care&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;12400 High Bluff Drive &lt;/li&gt;    &lt;li&gt;San Diego, CA 92130&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Wednesday Night&lt;/strong&gt; &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Microsoft &lt;/li&gt;    &lt;li&gt;Three Park Plaza, Suite 1800 &lt;/li&gt;    &lt;li&gt;Irvine, CA 92614&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Thursday Night&lt;/strong&gt; &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;American Honda Motors &lt;/li&gt;    &lt;li&gt;Bldg 100, # 100-1E-13 &lt;/li&gt;    &lt;li&gt;1919 Torrance Blvd &lt;/li&gt;    &lt;li&gt;Torrance, CA 90501&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href=" http://code.msdn.microsoft.com/linqfarm"&gt;Source and deck from the talk.&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f04%2f29%2fcharlie-speaking-in-southern-california.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%2f04%2f29%2fcharlie-speaking-in-southern-california.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8441737" 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/default.aspx">Community</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>LINQFarm: Understanding IEnumerable&lt;T&gt;, Part I</title><link>http://blogs.msdn.com/charlie/archive/2008/04/28/linqfarm-understanding-ienumerable-t-sets-and-sequences.aspx</link><pubDate>Mon, 28 Apr 2008 22:15:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8329924</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8329924.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8329924</wfw:commentRss><description>&lt;p&gt;The &lt;strong&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/strong&gt; interface is a key part of LINQ to Objects and binds many of its different features together into a whole. This series of posts explains &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt; and the role it plays in LINQ to Objects. If you hear people talking about &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt;, and sometimes wished you better understood its significance, then you should find this text helpful.&lt;/p&gt;  &lt;h2&gt;Collections and IEnumerable&amp;lt;T&amp;gt;&lt;/h2&gt;  &lt;p&gt;Though LINQ to Objects can be used to query several C# types, it cannot be used against all your in-process data sources. Those that can be queried all support the &lt;strong&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/strong&gt; interface. These include the generic collections found in the &lt;b&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/system.collections.generic.aspx"&gt;System.Collections.Generic&lt;/a&gt; &lt;/b&gt;namespace&lt;b&gt;. &lt;/b&gt;The commonly used types found in this namespace include &lt;b&gt;List&amp;lt;T&amp;gt;&lt;/b&gt;, &lt;b&gt;Stack&amp;lt;T&amp;gt;&lt;/b&gt;, &lt;b&gt;LinkedList&amp;lt;T&amp;gt;&lt;/b&gt;, &lt;b&gt;Queue&amp;lt;T&amp;gt;&lt;/b&gt;, &lt;b&gt;Dictionary&amp;lt;TKey, Value&amp;gt;&lt;/b&gt; and &lt;b&gt;Hashset&amp;lt;T&amp;gt;&lt;/b&gt;. &lt;/p&gt;  &lt;p&gt;All of the collections in the &lt;b&gt;System.Collections.Generic&lt;/b&gt; namespace support the &lt;b&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/b&gt; interface. Here, for instance, is the declaration for &lt;b&gt;List&amp;lt;T&amp;gt;:&lt;/b&gt;&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;T&amp;gt;, &lt;span style="color: #2b91af"&gt;ICollection&lt;/span&gt;&amp;lt;T&amp;gt;, &lt;span style="color: #2b91af"&gt;&lt;strong&gt;IEnumerable&lt;/strong&gt;&lt;/span&gt;&amp;lt;T&amp;gt;, &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;ICollection&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;You will find &lt;strong&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/strong&gt; listed for all the other generic collections. It is no coincidence that these collections support &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt;. Their implementation of this interface makes it possible to query them using LINQ to Objects.&lt;/p&gt;

&lt;h2&gt;LINQ to Objects and IEnumerable&amp;lt;T&amp;gt;&lt;/h2&gt;

&lt;p&gt;Consider the following simple LINQ query:&lt;/p&gt;

&lt;pre class="code"&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; list = &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, 3, 2 };&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;font color="#008000"&gt;// The LINQ Query expression&lt;/font&gt;
&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; num &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; list
            &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; num &amp;lt; 3
            &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; num;&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; query)
{
    &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(item);
}&lt;/pre&gt;

&lt;p&gt;The type &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt; plays two key roles in this code.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The query expression has a data source called &lt;strong&gt;list&lt;/strong&gt; which implements &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt;.&amp;#160; &lt;/li&gt;

  &lt;li&gt;The query expression returns an instance of &lt;strong&gt;IEnumberable&lt;/strong&gt;&amp;lt;T&amp;gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every LINQ to Objects query expression, including the one shown above, will begin with a line of this type:&lt;/p&gt;

&lt;p&gt;&lt;font color="#0000ff"&gt;from&lt;/font&gt; &lt;strong&gt;x&lt;/strong&gt; &lt;font color="#0000ff"&gt;in&lt;/font&gt; &lt;strong&gt;y&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In each case, the data source represented by the variable &lt;strong&gt;y&lt;/strong&gt; must support the &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt; interface. As you have already seen, the list of integers shown in this example supports that interface.&lt;/p&gt;

&lt;p&gt;The same query shown here could also be written as follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt; query = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; num &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; list
                         &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; num &amp;lt; 3
                         &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; num;&lt;/pre&gt;

&lt;p&gt;This code makes explicit the type of the variable returned by this query. As you can see, it is of type &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;int&amp;gt;. In practice, you will find that most LINQ to Objects queries return &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt;, for some type &lt;strong&gt;T&lt;/strong&gt;. The only exceptions are those that call a LINQ query operator that return a simple type, such as &lt;strong&gt;Count():&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; number = (&lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; num &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; list
              &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; num &amp;lt; 3
              &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; num).Count();&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;In this case the query returns an integer specifying the number of items in the list created by this query. LINQ queries that return a simple type like this are an exception to the rule that LINQ to Objects queries operate on class that implement &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt; and return an instance that supports &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt;. &lt;/p&gt;

&lt;h2&gt;Composable&lt;/h2&gt;

&lt;p&gt;The fact that LINQ to Objects queries both take and return &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt; enables a key feature of LINQ called composability. Because LINQ queries are composable you can usually pass the result of one LINQ query to another LINQ query. This allows you to compose a series of queries that work together to achieve a single end:&lt;/p&gt;

&lt;pre class="code"&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; list = &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, 3, 2 };

&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; query1 = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; num &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; list
             &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; num &amp;lt; 3
             &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; num;

&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; query2 = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; num &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; query1
             &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; num &amp;gt; 1
             &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; num;

&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; query3 = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; num1 &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; query1
             &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; num2 &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; query2
             &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; num1 + num2;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here the results of the first query are used as the data source for the second query, and the results of the first two queries are both used as data sources for the third query. If you print out the results of query3 with a &lt;strong&gt;foreach&lt;/strong&gt; loop you get the numbers 3 and 4. Though it is not important to the current subject matter, you might have fun playing with the code to understand why these values are returned.&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;By now it should be clear to you that &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt; plays a central role in LINQ to Objects. A typical LINQ to Objects query expression not only takes a class that implements &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt; as its data source, but it also returns an instance of this same type. The fact that it takes and returns the same type enables a feature called composability.&lt;/p&gt;

&lt;p&gt;The next logical question would be to ask why this type plays such a key role in LINQ to Objects. One simple answer would be that the creators of LINQ decided that it should be so, and hence it is so. But one can still ask why they picked this particular type. What is it about &lt;strong&gt;IEnumerable&lt;/strong&gt;&amp;lt;T&amp;gt; that makes it a useful data source and return type for LINQ to Objects queries? The answer to that question will be found in the second part of this series of articles.&lt;/p&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f04%2f28%2flinqfarm-understanding-ienumerable-t-sets-and-sequences.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%2f04%2f28%2flinqfarm-understanding-ienumerable-t-sets-and-sequences.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8329924" 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's Deck and Demos from his Essence of LINQ Talk</title><link>http://blogs.msdn.com/charlie/archive/2008/04/07/charlie-s-deck-and-demos-from-his-essence-of-linq-talk.aspx</link><pubDate>Tue, 08 Apr 2008 02:49:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8366920</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/charlie/comments/8366920.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=8366920</wfw:commentRss><description>&lt;p&gt;The sample programs and the slides from my Essence of LINQ talk are available on the Code Gallery LINQ Farm resource page.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;The LINQ Farm Resource Page&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=linqfarm&amp;amp;ReleaseId=81"&gt;The Downloads section of the page&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=linqfarm&amp;amp;DownloadId=1575"&gt;Direct Link to the Download&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;/p&gt; &lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f04%2f07%2fcharlie-s-deck-and-demos-from-his-essence-of-linq-talk.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%2f04%2f07%2fcharlie-s-deck-and-demos-from-his-essence-of-linq-talk.aspx" border="0" /&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8366920" 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 Seed: Using the Expression Tree Visualizer</title><link>http://blogs.msdn.com/charlie/archive/2008/02/13/linq-farm-seed-using-the-expression-tree-visualizer.aspx</link><pubDate>Wed, 13 Feb 2008 21:54:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7679083</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>19</slash:comments><comments>http://blogs.msdn.com/charlie/comments/7679083.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=7679083</wfw:commentRss><description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;The Visual Studio 2008 CSharp samples include several valuable tools that LINQ developers can use to help expedite the development process. One of the is the Expression Tree Visualizer. This tool works in both Visual Studio Express and the other versions of Visual Studio that support C# development.&lt;/p&gt; &lt;p&gt;&lt;em&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you are unfamiliar with expression trees, then you might want to view &lt;/em&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2008/01/31/expression-tree-basics.aspx"&gt;&lt;em&gt;this post&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt; &lt;p&gt;You can access the Expression Tree Visualizer by choosing &lt;strong&gt;Help&lt;/strong&gt; | &lt;strong&gt;Samples&lt;/strong&gt; from the Visual Studio 2008 menu system. From there you will be able to access links to the &lt;a href="http://code.msdn.microsoft.com/csharpsamples"&gt;updated online version of the samples&lt;/a&gt;. &lt;/p&gt; &lt;p&gt;After you unzip the samples you should navigate to the &lt;strong&gt;LinqSamples&lt;/strong&gt; directory, open the &lt;strong&gt;ExpressionTreeVisualizer&lt;/strong&gt; project, and build it. By default, F6 is the build key in Visual Studio 2008.&lt;/p&gt; &lt;p&gt;After the build process is complete a library called ExpressionTreeVisualizer.dll will be created in the following directory:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;...\LinqSamples\ExpressionTreeVisualizer\ExpressionTreeVisualizer\bin\Debug&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Copy this DLL to your &lt;strong&gt;Visualizers&lt;/strong&gt; directory. If the directory does not exist, you can create it inside the default user directory for Visual Studio. That directory would typically be located here:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;...\Documents\Visual Studio 2008\Visualizers&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;One Windows XP system, the path might look like this:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;...\My Documents\Visual Studio 2008\Visualizers&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The &lt;strong&gt;Code Snippets,&lt;/strong&gt; &lt;strong&gt;Projects&lt;/strong&gt; and &lt;strong&gt;Templates&lt;/strong&gt; directories also appear in this same location. You can also find the location of this directory by selecting &lt;strong&gt;Tools | Options | Projects and Solutions | General&lt;/strong&gt; from the Visual Studio 2008 menu. You may need to restart Visual Studio after copying the ExpressionTreeVisualizer library to the &lt;strong&gt;Visualizers&lt;/strong&gt; directory.&lt;/p&gt; &lt;p&gt;You can now test the visualizer by opening a project that creates an expression tree such as the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;Expression Tree Basics&lt;/a&gt; sample from the &lt;a href="http://code.msdn.microsoft.com/"&gt;Code Gallery&lt;/a&gt;. Alternatively, you can create a default console application, add &lt;strong&gt;System.Linq.Expressions&lt;/strong&gt; to the using statements, and add two lines of code to the program body:&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.Linq.Expressions;

&lt;span style="color: rgb(0,0,255)"&gt;namespace&lt;/span&gt; ConsoleApplication107
{
    &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;Expression&lt;/span&gt;&amp;lt;&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;&amp;gt; expression = (a, b) =&amp;gt; a + b;

            &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(expression);
        }
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;If you are using the code shown above, set a breakpoint on the &lt;strong&gt;WriteLine&lt;/strong&gt; statement and run the program. Hover your mouse under the variable &lt;strong&gt;expression&lt;/strong&gt;, as shown in Figure 1. A small Quick Info box with a magnifying glass will appear.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="194" alt="ExpressionTreeMagnifyingGlass" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/LINQFarmSeedUsingtheExpressionTreeVisual_7D54/ExpressionTreeMagnifyingGlass_3.png" width="376" border="0"&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Figure 1: Examining the variable &lt;u&gt;expression&lt;/u&gt; at run time in Visual Studio with Quick Info.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you click on the magnifying class, an option to view the Expression Tree Visualizer will appear, as shown in Figure 2.&lt;/p&gt;
&lt;p&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="111" alt="ExpressionTreeMagnifyingGlass2" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/LINQFarmSeedUsingtheExpressionTreeVisual_7D54/ExpressionTreeMagnifyingGlass2_3.png" width="394" border="0"&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Figure 2: By clicking on the magnifying class you can get access to the Expression Tree Visualizer.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you select the visualizer option then it will appear and you can view the nodes of your expression tree, as shown in Figure 3.&lt;/p&gt;
&lt;p&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="731" alt="ExpressionTreeMagnifyingGlass3" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/LINQFarmSeedUsingtheExpressionTreeVisual_7D54/ExpressionTreeMagnifyingGlass3_3.png" width="422" border="0"&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Figure 3: A visualization of the expression tree for a simple lambda expression.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you are working with a LINQ to SQL query, then can use the same technique to view an expression tree. You will, however, need to take one extra step because the expression tree is a member of the IQueryable variable returned from a typical LINQ to SQL query expression. Consider this simple LINQ to SQL query:&lt;/p&gt;&lt;pre class="code"&gt;&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; db.Customers
            &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; c.City == &lt;span style="color: rgb(163,21,21)"&gt;"London"
&lt;/span&gt;            &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; c;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;At runtime, you can get Quick Info on the variable &lt;strong&gt;query&lt;/strong&gt;, much as we did on the variable &lt;strong&gt;expression&lt;/strong&gt; earlier in this post. Figure 4 shows how it looks:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/LINQFarmSeedUsingtheExpressionTreeVisual_7D54/ExpressionTreeMagnifyingGlass4_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="123" alt="ExpressionTreeMagnifyingGlass4" src="http://blogs.msdn.com/blogfiles/charlie/WindowsLiveWriter/LINQFarmSeedUsingtheExpressionTreeVisual_7D54/ExpressionTreeMagnifyingGlass4_thumb.png" width="484" border="0"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Figure 4: Hover the mouse over the variable &lt;u&gt;query&lt;/u&gt;, then drill down to view the expression tree which is found in the &lt;u&gt;queryExpression&lt;/u&gt; field. (Double click on this image to see a full-sized version.)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When you select the magnifying class, you will be presented with an option to pop up the Expression Tree Visualizer, which will look like the image shown in Figure 3, but with different data.&lt;/p&gt;
&lt;h2&gt;Summary &lt;/h2&gt;
&lt;p&gt;This post describes how to use the Expression Tree Visualizer that ships with the Visual Studio 2008 samples. &lt;/p&gt;
&lt;p&gt;See also:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2008/01/31/expression-tree-basics.aspx"&gt;Expression Tree Basics&lt;/a&gt; 
&lt;li&gt;&lt;a href="http://blogs.msdn.com/charlie/archive/2007/03/05/march-ctp-samples-overview-video.aspx"&gt;A (badly dated) overview of the samples&lt;/a&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%2f02%2f13%2flinq-farm-seed-using-the-expression-tree-visualizer.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%2f02%2f13%2flinq-farm-seed-using-the-expression-tree-visualizer.aspx" border="0"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7679083" 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 Seed 02: Aggregate Operator Part II</title><link>http://blogs.msdn.com/charlie/archive/2008/02/06/linq-farm-seed-02-aggregate-operator-part-ii.aspx</link><pubDate>Thu, 07 Feb 2008 00:08:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7483298</guid><dc:creator>Charlie Calvert</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/charlie/comments/7483298.aspx</comments><wfw:commentRss>http://blogs.msdn.com/charlie/commentrss.aspx?PostID=7483298</wfw:commentRss><description>&lt;p&gt;LINQ Farm Seeds are short posts designed to be read in a few minutes. In the previous &lt;a href="http://blogs.msdn.com/charlie/archive/2008/02/05/linq-farm-seed-i-aggregate-operator-part-i.aspx"&gt;seed&lt;/a&gt; we used the first overload of the C# 3.0 LINQ &lt;strong&gt;Aggregate&lt;/strong&gt; operator to sum a list of numbers. In this post we'll work with a list of words. &lt;/p&gt; &lt;p&gt;The code we will look at reverses the following string:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;The end is the beginning, the beginning the end&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;If we pass this string in to our method, it will yield this result:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;end the beginning the beginning, the is end The&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Here is the method that we will use:&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;string&lt;/span&gt; ReverseSentence(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; sentence)
{
&lt;span style="color: rgb(0,0,255)"&gt;  string&lt;/span&gt;[] words = sentence.Split(&lt;span style="color: rgb(163,21,21)"&gt;' '&lt;/span&gt;);           

  &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; words.Aggregate((accumulator, b) =&amp;gt; b + &lt;span style="color: rgb(163,21,21)"&gt;" "&lt;/span&gt; + accumulator);
}&lt;/pre&gt;
&lt;p&gt;The method first breaks the sentence down into a list of words:&lt;/p&gt;
&lt;p&gt;&lt;span style="color: rgb(0,0,255)"&gt;&amp;nbsp; string&lt;/span&gt;[] words = sentence.Split(&lt;span style="color: rgb(163,21,21)"&gt;' '&lt;/span&gt;);&lt;/p&gt;
&lt;p&gt;It passes this list to the LINQ to Objects &lt;strong&gt;Aggregate&lt;/strong&gt; operator. This overload of that operator takes a single lambda expression as a parameter. The lambda expression takes two parameters (accumulator, b) and returns them in reverse order (b accumulator). If it were a standard method rather than a lambda expression, it would look like this:&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;string&lt;/span&gt; Reverse(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; accumulator, &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; b)
{
&lt;span style="color: rgb(0,0,255)"&gt;   return&lt;/span&gt; b + &lt;span style="color: rgb(163,21,21)"&gt;" "&lt;/span&gt; + accumulator;
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The method would be called like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp; words.Aggregate(Reverse); &lt;/p&gt;
&lt;p&gt;In our code, however, we use lambda expressions rather than standard methods. &lt;/p&gt;
&lt;p&gt;As you recall from the previous post, the &lt;strong&gt;Aggregate&lt;/strong&gt; operators passes in items two at a time from a list. The first parameter becomes the accumulator for the aggregation. This is why I chose to give the first parameter in the lambda expression the name accumulator:&lt;/p&gt;
&lt;p&gt;&lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; words.Aggregate((accumulator, b) =&amp;gt; b + &lt;span style="color: rgb(163,21,21)"&gt;" "&lt;/span&gt; + accumulator); &lt;/p&gt;
&lt;p&gt;You can watch the aggregation process take place in the debugger. Set a breakpoint on the call to &lt;strong&gt;Aggregate&lt;/strong&gt;, and add the variables &lt;strong&gt;accumulator&lt;/strong&gt; and &lt;strong&gt;b&lt;/strong&gt; to your watch list. Step through the method with the F11 key. You can watch the result "accumulate" in the accumulator variable.&lt;/p&gt;
&lt;p&gt;The process begins by passing the first two words into the lambda expression, where the first one is the accumulator:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(The) (end)&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;The method reverses their order and places a space between them:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;end The &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;The next iteration sends in the result of the previous expression, plus the next word from the list:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(end the) (is)&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;The lambda expression reverses them:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;is end The&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;The Aggregate operator now processes the result of the previous iteration in the accumulator plus the next word:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(is end The) (the)&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Again our lambda expression swaps the parameters and places a space between them:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the is end The&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Next time we pass in the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(the is end The) (beginning,)&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;This yields:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;beginning, the is end The&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;The rest of the cycle looks like this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(beginning, the is end The), (the) &amp;gt;===&amp;gt; the beginning, the is end The 
&lt;li&gt;(the beginning, the is end The) (beginning) &amp;gt;===&amp;gt; beginning the beginning, the is end The 
&lt;li&gt;(beginning the beginning, the is end The) (the) &amp;gt;===&amp;gt; the beginning the beginning, the is end The 
&lt;li&gt;(the beginning the beginning, the is end The) (end) &amp;gt;===&amp;gt; end the beginning the beginning, the is end The&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;I find this an immensely satisfying process to contemplate simply because it so very logically runs counter to my intuitions. I keep expecting it to do something different, but when I think it through I see exactly why it does what it does. How many real world uses for this I will discover I don't know, but I will treasure each one as I encounter it.&lt;/p&gt;
&lt;p&gt;Listing 1 shows the complete code for this simple program. In this version of the ReverseSentence method, you see how to convert the sentence int &lt;strong&gt;List&lt;/strong&gt;&amp;lt;&lt;strong&gt;string&lt;/strong&gt;&amp;gt; rather than an array of string. I use the &lt;strong&gt;ToList&lt;/strong&gt;() LINQ operator to perform this conversion, so you are seeing a sneak peak of an operator I haven't yet covered in this series.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Listing 01: This code shows how to use the &lt;u&gt;Aggregate()&lt;/u&gt; and &lt;u&gt;ToList()&lt;/u&gt; operators that are declared in the &lt;u&gt;Enumerator&lt;/u&gt; class and which are part of LINQ to Objects.&lt;/strong&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; LinqFarmSeed02
{
    &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;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; ReverseString(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; sentence)
        {
            &lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt;&amp;gt; words = &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;string&lt;/span&gt;&amp;gt;(sentence.Split(&lt;span style="color: rgb(163,21,21)"&gt;' '&lt;/span&gt;).ToList());

            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; words.Aggregate((a, b) =&amp;gt; b + &lt;span style="color: rgb(163,21,21)"&gt;" "&lt;/span&gt; + a);            
        }

        &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(0,0,255)"&gt;string&lt;/span&gt; sentence = &lt;span style="color: rgb(163,21,21)"&gt;"The end is the beginning, the beginning the end"&lt;/span&gt;;
            &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(sentence);

            &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; result = ReverseString(sentence);
            &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(result);
        }        
    }
}&lt;/pre&gt;
&lt;p&gt;In this post you have seen how to use the &lt;strong&gt;Aggregate&lt;/strong&gt; operator to reverse the order of a list of words. Thinking through the way this operator works on a list of strings can help you understand exactly how it is implemented and exactly what it does. &lt;/p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;-----------------&lt;/p&gt;
&lt;p&gt;Download the source code from the &lt;a href="http://code.msdn.microsoft.com/linqfarm"&gt;LINQ Farm&lt;/a&gt;. 
&lt;p&gt;I want to get my hands dirty; show me more &lt;a href="http://blogs.msdn.com/charlie/archive/2006/11/11/charlie-calvert-s-technical-blog-index.aspx"&gt;LINQ Farm posts&lt;/a&gt;.&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2fcharlie%2farchive%2f2008%2f02%2f06%2flinq-farm-seed-02-aggregate-operator-part-ii.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%2f02%2f06%2flinq-farm-seed-02-aggregate-operator-part-ii.aspx" border="0"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7483298" 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></channel></rss>