<?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>C# Frequently Asked Questions</title><link>http://blogs.msdn.com/csharpfaq/default.aspx</link><description>The C# team posts answers to common questions and describes new language features</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Debugging Expression Trees in Visual Studio 2010</title><link>http://blogs.msdn.com/csharpfaq/archive/2009/11/19/debugging-expression-trees-in-visual-studio-2010.aspx</link><pubDate>Thu, 19 Nov 2009 20:05:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9925713</guid><dc:creator>Alexandra Rusina</dc:creator><slash:comments>14</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/9925713.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=9925713</wfw:commentRss><description>&lt;p&gt;First of all, let’s take a look at the example from one of &lt;a href="http://go.microsoft.com/fwlink/?LinkID=169513"&gt;my previous posts&lt;/a&gt;. It creates an expression tree for calculating the factorial of a number.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ParameterExpression &lt;/span&gt;value = 
    &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Parameter(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;int&lt;/span&gt;), &lt;span style="color: #a31515"&gt;&amp;quot;value&amp;quot;&lt;/span&gt;);
&lt;span style="color: #2b91af"&gt;ParameterExpression &lt;/span&gt;result = 
    &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Parameter(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;int&lt;/span&gt;), &lt;span style="color: #a31515"&gt;&amp;quot;result&amp;quot;&lt;/span&gt;);
&lt;span style="color: #2b91af"&gt;LabelTarget &lt;/span&gt;label = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Label(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;int&lt;/span&gt;));
&lt;span style="color: #2b91af"&gt;BlockExpression &lt;/span&gt;block = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Block(
    &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { result },
    &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Assign(result, &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Constant(&lt;span style="color: brown"&gt;1&lt;/span&gt;)),
    &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Loop(
        &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.IfThenElse(
            &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.GreaterThan(value, 
                &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Constant(&lt;span style="color: brown"&gt;1&lt;/span&gt;)),
            &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.MultiplyAssign(result,
                &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.PostDecrementAssign(value)),
        &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Break(label, result)
        ),
        label
    )
);
&lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;&amp;gt; lambda = 
    &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Lambda&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;&amp;gt;(block, value);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(lambda.Compile()(&lt;span style="color: brown"&gt;5&lt;/span&gt;));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now, what if you want to see the content of this tree in debug mode? You can try &lt;font face="Courier New"&gt;lambda.ToString()&lt;/font&gt; but it’s not very informative. All you get is this: &lt;font face="Courier New"&gt;value =&amp;gt; {var result; … }&lt;/font&gt;. Basically, it tells you only that the lambda has the &lt;font face="Courier New"&gt;result&lt;/font&gt; parameter.&lt;/p&gt;

&lt;p&gt;Another option is to explore the tree structure in the &lt;b&gt;Watch&lt;/b&gt; window. However, since this is a tree, you need to click through numerous nodes. This gives you a good understanding of the tree structure, but it might be hard to understand the actual content of the tree.&lt;/p&gt;

&lt;p&gt;But there is a better solution: expression trees now have their own visualizer that helps you to explore their structure and content. &lt;/p&gt;

&lt;p&gt;Copy the above code example into Visual Studio 2010, set a breakpoint at the last line, and press F5. Rest the mouse pointer over the lambda variable and you will see that it has the &lt;strong&gt;DebugView&lt;/strong&gt; property. This property is private and is exposed only in the debugger, so you can’t use it in your code. But it’s used to hook up the expression trees visualizer. Click the magnifying glass icon and you will see the list of available visualizers.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/csharpfaq/WindowsLiveWriter/DebuggingExpressionTreesinVisualStudio20_AA02/image_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="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/csharpfaq/WindowsLiveWriter/DebuggingExpressionTreesinVisualStudio20_AA02/image_thumb.png" width="522" height="164" /&gt;&lt;/a&gt; &lt;/p&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;Now click &lt;b&gt;Text Visualizer&lt;/b&gt; and you get this.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/csharpfaq/WindowsLiveWriter/DebuggingExpressionTreesinVisualStudio20_AA02/image_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="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/csharpfaq/WindowsLiveWriter/DebuggingExpressionTreesinVisualStudio20_AA02/image_thumb_1.png" width="524" height="292" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you can see, the content of the expression tree is represented in some kind of metalanguage. (No, we couldn't use C# here, because expression trees serve other languages as well.)&lt;/p&gt;

&lt;p&gt;There are some simple rules used in this metalanguage:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Parameters are displayed with a $ symbol at the beginning (for example, $result and $value.) &lt;/li&gt;

  &lt;li&gt;Integer, string, and null constants are displayed “as is” (for example, $result = 1). For other numeric types that have standard suffixes, the suffix is added to the value. (For example, for decimal the value is 1M, for double 1D, etc.) &lt;/li&gt;

  &lt;li&gt;If a parameter, label, or lambda expression does not have a name, the name is generated automatically (for example, #Label1, $var1, etc.) &lt;/li&gt;

  &lt;li&gt;Checked operators are displayed with the # symbol preceding the operator. For example, the checked addition operator is displayed as #+. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are some other nuances that I think are evident and don't need additional explanation. But if you don’t understand something in this syntax, let me know and I'll include more information in the documentation.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9925713" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/Visual+Studio+2010/default.aspx">Visual Studio 2010</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/expression+trees/default.aspx">expression trees</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/DLR/default.aspx">DLR</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/.NET+Framework+4.0/default.aspx">.NET Framework 4.0</category></item><item><title>Dynamic in C# 4.0: Creating Wrappers with DynamicObject</title><link>http://blogs.msdn.com/csharpfaq/archive/2009/10/19/dynamic-in-c-4-0-creating-wrappers-with-dynamicobject.aspx</link><pubDate>Mon, 19 Oct 2009 21:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9909448</guid><dc:creator>Alexandra Rusina</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/9909448.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=9909448</wfw:commentRss><description>&lt;P&gt;In the &lt;A href="http://blogs.msdn.com/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the-expandoobject.aspx" mce_href="http://blogs.msdn.com/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the-expandoobject.aspx"&gt;previous post&lt;/A&gt; I showed how you can use the new dynamic feature and the &lt;A href="http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(VS.100).aspx"&gt;ExpandoObject&lt;/A&gt; class to add and remove properties at run time, and how this can make your code more readable and flexible than code written with LINQ to XML syntax. &lt;/P&gt;
&lt;P&gt;But there were some obvious flaws in that example: While &lt;B&gt;ExpandoObject&lt;/B&gt; provided better syntax, LINQ to XML provided a lot of useful library methods that helped you to work with XML files. So, is it possible to combine those two advantages, to have better syntax and still get all those methods? The answer is yes, but you need another type from the &lt;B&gt;System.Dynamic&lt;/B&gt; namespace: &lt;A href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject(VS.100).aspx"&gt;DynamicObject&lt;/A&gt;&lt;B&gt;.&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;The &lt;B&gt;DynamicObject&lt;/B&gt; class enables you to override operations like getting or setting a member, calling a method, or performing any binary, unary, or type conversion operation. To illustrate the issue, let’s create a very simple object that overrides the “get property” operation, so that whenever you access a property it returns the property’s name as a string. This example has no practical value.&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;SampleObject &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;DynamicObject
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public override bool &lt;/SPAN&gt;TryGetMember(
        &lt;SPAN style="COLOR: #2b91af"&gt;GetMemberBinder &lt;/SPAN&gt;binder, &lt;SPAN style="COLOR: blue"&gt;out object &lt;/SPAN&gt;result)
    {
        result = binder.Name;
        &lt;SPAN style="COLOR: blue"&gt;return true&lt;/SPAN&gt;;
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;As with &lt;B&gt;ExpandoObject&lt;/B&gt;, we must use the &lt;B&gt;dynamic&lt;/B&gt; keyword to create an instance of this class.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;dynamic &lt;/SPAN&gt;obj = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SampleObject&lt;/SPAN&gt;();
&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(obj.SampleProperty);
&lt;SPAN style="COLOR: green"&gt;//Prints "SampleProperty".&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;Let’s see what’s going on in this example. When you call &lt;FONT face="Courier New"&gt;obj.SampleProperty&lt;/FONT&gt;, the &lt;A href="http://msdn.microsoft.com/en-us/library/dd233052(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd233052(VS.100).aspx"&gt;dynamic language runtime&lt;/A&gt; (DLR) uses the language binder to look for a static definition of this property in the &lt;B&gt;SampleObject&lt;/B&gt; class. If there is no such property, the DLR calls the &lt;B&gt;TryGetMember&lt;/B&gt; method. This method gets information about what property it was called for through the &lt;FONT face="Courier New"&gt;binder&lt;/FONT&gt; parameter. As you can see, the &lt;FONT face="Courier New"&gt;binder.Name&lt;/FONT&gt; contains the actual name of the property.&lt;/P&gt;
&lt;P&gt;The &lt;B&gt;TryGetMember&lt;/B&gt; method returns &lt;FONT face="Courier New"&gt;true&lt;/FONT&gt; if the operation is successful. But the actual result of the operation must be assigned to the out parameter &lt;FONT face="Courier New"&gt;result&lt;/FONT&gt;. In this example, &lt;B&gt;TryGetMember&lt;/B&gt; returns &lt;FONT face="Courier New"&gt;true&lt;/FONT&gt;, but &lt;FONT face="Courier New"&gt;obj.SampleProperty&lt;/FONT&gt; returns "SampleProperty". &lt;/P&gt;
&lt;P&gt;Now let’s move to a more complex example and create a wrapper for the &lt;B&gt;XElement&lt;/B&gt; object. Once again, I’ll try to provide better syntax for the following LINQ to XML sample.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement &lt;/SPAN&gt;contactXML =
    &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Contact"&lt;/SPAN&gt;,
    &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Name"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Patrick Hines"&lt;/SPAN&gt;),
    &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Phone"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"206-555-0144"&lt;/SPAN&gt;),
    &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Address"&lt;/SPAN&gt;,
        &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Street1"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"123 Main St"&lt;/SPAN&gt;),
        &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"City"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Mercer Island"&lt;/SPAN&gt;),
        &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"State"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"WA"&lt;/SPAN&gt;),
        &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Postal"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"68042"&lt;/SPAN&gt;)
    )
);&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;First of all, I need to create an analog of &lt;B&gt;ExpandoObject&lt;/B&gt;. I still want to be able to dynamically add and remove properties. But since I am essentially creating a wrapper for the &lt;B&gt;XElement&lt;/B&gt; type, I’ll use &lt;B&gt;XElement&lt;/B&gt; instead of the dictionary to maintain the properties.&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;DynamicXMLNode &lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;DynamicObject
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: #2b91af"&gt;XElement &lt;/SPAN&gt;node;
    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;DynamicXMLNode(&lt;SPAN style="COLOR: #2b91af"&gt;XElement &lt;/SPAN&gt;node)
    {
        &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.node = node;
    }
    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;DynamicXMLNode()
    {
    }
    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;DynamicXMLNode(&lt;SPAN style="COLOR: #2b91af"&gt;String &lt;/SPAN&gt;name)
    {
        node = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(name);
    }
    &lt;SPAN style="COLOR: blue"&gt;public override bool &lt;/SPAN&gt;TrySetMember(
        &lt;SPAN style="COLOR: #2b91af"&gt;SetMemberBinder &lt;/SPAN&gt;binder, &lt;SPAN style="COLOR: blue"&gt;object &lt;/SPAN&gt;value)
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;XElement &lt;/SPAN&gt;setNode = node.Element(binder.Name);
        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(setNode != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
            setNode.SetValue(value);
        &lt;SPAN style="COLOR: blue"&gt;else
        &lt;/SPAN&gt;{
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(value.GetType() == &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;DynamicXMLNode&lt;/SPAN&gt;))
                node.Add(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(binder.Name));
            &lt;SPAN style="COLOR: blue"&gt;else
                &lt;/SPAN&gt;node.Add(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(binder.Name, value));
        }
        &lt;SPAN style="COLOR: blue"&gt;return true&lt;/SPAN&gt;;
    }
    &lt;SPAN style="COLOR: blue"&gt;public override bool &lt;/SPAN&gt;TryGetMember(&lt;BR&gt;        &lt;SPAN style="COLOR: #2b91af"&gt;GetMemberBinder &lt;/SPAN&gt;binder, &lt;SPAN style="COLOR: blue"&gt;out object &lt;/SPAN&gt;result)
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;XElement &lt;/SPAN&gt;getNode = node.Element(binder.Name);
        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(getNode != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
        {
            result = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DynamicXMLNode&lt;/SPAN&gt;(getNode);
            &lt;SPAN style="COLOR: blue"&gt;return true&lt;/SPAN&gt;;
        }
        &lt;SPAN style="COLOR: blue"&gt;else
        &lt;/SPAN&gt;{
            result = &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;;
            &lt;SPAN style="COLOR: blue"&gt;return false&lt;/SPAN&gt;;
        }
    }
}&lt;/PRE&gt;
&lt;P&gt;And here is how you can use this class.&lt;/P&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;dynamic &lt;/SPAN&gt;contact = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DynamicXMLNode&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Contacts"&lt;/SPAN&gt;);
contact.Name = &lt;SPAN style="COLOR: #a31515"&gt;"Patrick Hines"&lt;/SPAN&gt;;
contact.Phone = &lt;SPAN style="COLOR: #a31515"&gt;"206-555-0144"&lt;/SPAN&gt;;
contact.Address = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DynamicXMLNode&lt;/SPAN&gt;();
contact.Address.Street = &lt;SPAN style="COLOR: #a31515"&gt;"123 Main St"&lt;/SPAN&gt;;
contact.Address.City = &lt;SPAN style="COLOR: #a31515"&gt;"Mercer Island"&lt;/SPAN&gt;;
contact.Address.State = &lt;SPAN style="COLOR: #a31515"&gt;"WA"&lt;/SPAN&gt;;
contact.Address.Postal = &lt;SPAN style="COLOR: #a31515"&gt;"68402"&lt;/SPAN&gt;;&lt;/PRE&gt;
&lt;P&gt;Let’s look at the &lt;FONT face="Courier New"&gt;contact&lt;/FONT&gt; object. When this object is created, it initializes its inner &lt;B&gt;XElement&lt;/B&gt;. If you set a property value, like in &lt;FONT face="Courier New"&gt;contact.Phone = &lt;SPAN style="COLOR: #a31515"&gt;"206-555-0144"&lt;/SPAN&gt;&lt;/FONT&gt;, the &lt;B&gt;TrySetMember&lt;/B&gt; method checks whether an element with the name Phone exists in its &lt;B&gt;XElement&lt;/B&gt;. If it does not exist, the method creates the element.&lt;/P&gt;
&lt;P&gt;The next interesting line is &lt;FONT face="Courier New"&gt;contact.Address = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DynamicXMLNode&lt;/SPAN&gt;().&lt;/FONT&gt; Basically, in this particular example this line means that I want to create a node that has subnodes. For this property, the &lt;B&gt;TrySetMember&lt;/B&gt; method creates an &lt;B&gt;XElement&lt;/B&gt; without a value.&lt;/P&gt;
&lt;P&gt;The most complex case here is a line such as &lt;FONT face="Courier New"&gt;contact.Address.State = &lt;FONT color=#000000&gt;&lt;SPAN style="COLOR: #a31515"&gt;"WA"&lt;/SPAN&gt;.&lt;/FONT&gt;&lt;/FONT&gt; First, the &lt;B&gt;TryGetMember&lt;/B&gt; method is called for &lt;FONT face="Courier New"&gt;contact.Address&lt;/FONT&gt; and returns a new &lt;B&gt;DynamicXMLNode&lt;/B&gt; object, which is initialized by the &lt;B&gt;XElement&lt;/B&gt; with the name Address. (Theoretically, I could have returned the &lt;B&gt;XElement&lt;/B&gt; itself, but that would make the example more complicated.) Then the &lt;B&gt;TrySetMember&lt;/B&gt; method is called. The method looks for the State element in &lt;FONT face="Courier New"&gt;contact.Address&lt;/FONT&gt;. If it doesn’t find one, it creates it.&lt;/P&gt;
&lt;P&gt;So I have successfully created the required XML structure. But &lt;B&gt;TryGetMember&lt;/B&gt; always returns an instance of &lt;B&gt;DynamicXMLNode&lt;/B&gt;. How do I get the actual value of the XML node? For example, I want the following line to work, but now it throws an exception.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;String &lt;/SPAN&gt;state = contact.Address.State;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;I have several options here. I can modify the &lt;B&gt;TryGetMember&lt;/B&gt; method to return actual values for leaf nodes, for example. But let’s explore another option: override type conversion. Just add the following method to the &lt;B&gt;DynamicXMLNode&lt;/B&gt; class.&lt;/P&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public override bool &lt;/SPAN&gt;TryConvert(
    &lt;SPAN style="COLOR: #2b91af"&gt;ConvertBinder &lt;/SPAN&gt;binder, &lt;SPAN style="COLOR: blue"&gt;out object &lt;/SPAN&gt;result)
{
    &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(binder.Type == &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;))
    {
        result = node.Value;
        &lt;SPAN style="COLOR: blue"&gt;return true&lt;/SPAN&gt;;
    }
    &lt;SPAN style="COLOR: blue"&gt;else
    &lt;/SPAN&gt;{
        result = &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;;
        &lt;SPAN style="COLOR: blue"&gt;return false&lt;/SPAN&gt;;
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Now whenever I have an explicit or implicit type conversion of the &lt;B&gt;DynamicXMLNode&lt;/B&gt; type, the &lt;B&gt;TryConvert&lt;/B&gt; method is called. The method checks what type the object is converted to and, if this type is &lt;B&gt;String&lt;/B&gt;, the method returns the value of the inner &lt;B&gt;XElement&lt;/B&gt;. Otherwise, it returns &lt;B&gt;false&lt;/B&gt;, which means that the language should determine what to do next (in most cases it means that you’re going to get a run-time exception).&lt;/P&gt;
&lt;P&gt;The last thing I’m going to show is how to get access to the &lt;B&gt;XElement&lt;/B&gt; methods. Let’s override the &lt;B&gt;TryInvokeMember&lt;/B&gt; method so that it will redirect all the method calls to its &lt;B&gt;XElement&lt;/B&gt; object. Of course, I’m using the &lt;STRONG&gt;System.Reflection&lt;/STRONG&gt; namespace here.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public override bool &lt;/SPAN&gt;TryInvokeMember(
    &lt;SPAN style="COLOR: #2b91af"&gt;InvokeMemberBinder &lt;/SPAN&gt;binder, &lt;BR&gt;    &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;[] args, &lt;BR&gt;    &lt;SPAN style="COLOR: blue"&gt;out object &lt;/SPAN&gt;result)
{
    &lt;SPAN style="COLOR: #2b91af"&gt;Type &lt;/SPAN&gt;xmlType = &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;);
    &lt;SPAN style="COLOR: blue"&gt;try
    &lt;/SPAN&gt;{
        result = xmlType.InvokeMember(
                  binder.Name,
                  BindingFlags.InvokeMethod |
                  BindingFlags.Public |
                  BindingFlags.Instance,
                  &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;, node, args);
        &lt;SPAN style="COLOR: blue"&gt;return true&lt;/SPAN&gt;;
    }
    &lt;SPAN style="COLOR: blue"&gt;catch
    &lt;/SPAN&gt;{
        result = &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;;
        &lt;SPAN style="COLOR: blue"&gt;return false&lt;/SPAN&gt;;
    }
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;This method enables you to call &lt;B&gt;XElement&lt;/B&gt; methods on any node of the &lt;B&gt;DynamicXMLNode&lt;/B&gt; object. The most obvious drawback here is absence of IntelliSense.&lt;/P&gt;
&lt;P&gt;I’m not even going to pretend that this example is a ready-to-use wrapper for the LINQ to XML library. It doesn’t support attributes, doesn’t allow you to create a collection of nodes (for example, multiple contacts), and it is probably missing other features. Creating a library is a difficult task, and creating a good wrapper is too. But I hope that after reading this blog post you can create a fully functioning wrapper with &lt;B&gt;DynamicObject&lt;/B&gt; yourself.&lt;/P&gt;
&lt;P&gt;So, if you routinely use a library with complicated syntax that crawls XML files or works with script objects, or if you are creating such a library yourself, you should probably consider writing a wrapper. Doing this might make you more productive and the syntax of your library much better.&lt;/P&gt;
&lt;P&gt;All the examples provided in this blog post work in the just released &lt;A&gt;&lt;/A&gt;&lt;A href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx" mce_href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;Visual Studio 2010 Beta 2&lt;/A&gt;. If you have any comments or suggestions, you are welcome to post them here or contact the DLR team at &lt;A href="http://www.codeplex.com/dlr" mce_href="http://www.codeplex.com/dlr"&gt;http://www.codeplex.com/dlr&lt;/A&gt;. You can also send an e-mail to the DLR team at &lt;A href="mailto:dlr@microsoft.com" mce_href="mailto:dlr@microsoft.com"&gt;dlr@microsoft.com&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject(VS.100).aspx"&gt;Documentation for &lt;STRONG&gt;DynamicObject&lt;/STRONG&gt;&lt;/A&gt; is also available on MSDN (check out our new MSDN design and don’t forget to take a look at the &lt;A href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject(VS.100,lightweight).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject(VS.100,lightweight).aspx"&gt;lightweight view&lt;/A&gt;.) In documentation, you can read about other useful methods of this class, such as &lt;A href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.trybinaryoperation(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.trybinaryoperation(VS.100).aspx"&gt;TryBinaryOperation&lt;/A&gt;, &lt;A href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.tryunaryoperation(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.tryunaryoperation(VS.100).aspx"&gt;TryUnaryOperation&lt;/A&gt;, &lt;A href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.trysetindex(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.trysetindex(VS.100).aspx"&gt;TrySetIndex&lt;/A&gt;, and &lt;A href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.trygetindex(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.trygetindex(VS.100).aspx"&gt;TryGetIndex&lt;/A&gt;. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9909448" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/Visual+Studio+2010/default.aspx">Visual Studio 2010</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/dynamic+language+runtime/default.aspx">dynamic language runtime</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/DLR/default.aspx">DLR</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/DynamicObject/default.aspx">DynamicObject</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category></item><item><title>Dynamic in C# 4.0: Introducing the ExpandoObject</title><link>http://blogs.msdn.com/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the-expandoobject.aspx</link><pubDate>Wed, 30 Sep 2009 23:49:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9901579</guid><dc:creator>Alexandra Rusina</dc:creator><slash:comments>58</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/9901579.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=9901579</wfw:commentRss><description>&lt;P&gt;You have probably already heard about the new dynamic feature in C# 4.0 and how it is used to support COM interop. If you haven't, I strongly recommend reading the following MSDN articles: &lt;A href="http://msdn.microsoft.com/en-us/library/dd264736(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd264736(VS.100).aspx"&gt;Using Type dynamic&lt;/A&gt; and &lt;A href="http://msdn.microsoft.com/en-us/library/dd264733(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd264733(VS.100).aspx"&gt;How to: Access Office Interop Objects by Using Visual C# 2010 Features&lt;/A&gt;. 
&lt;P&gt;Well, where else can you use this new feature? What are the use cases? Where does dynamic dispatch work better than static typing? 
&lt;P&gt;The quick answer is that whenever you see syntax like &lt;FONT face="Courier New"&gt;myobject.GetProperty("Address")&lt;/FONT&gt;, you have a use case for dynamic objects. First of all, the above syntax is difficult to read. Second, you don’t have any IntelliSense support for the property name, and if the “Address” property doesn’t exist you get a run-time exception. So why not create a dynamic object that calls the property as &lt;FONT face="Courier New"&gt;myobject.Address&lt;/FONT&gt;? You still get the run-time exception, and you still don't get IntelliSense, but at least the syntax is much better. 
&lt;P&gt;In fact, it&lt;A title=OLE_LINK2 name=OLE_LINK2&gt;&lt;/A&gt;&lt;A title=OLE_LINK1 name=OLE_LINK1&gt;&lt;/A&gt;’s not just better syntax. You also get flexibility. To demonstrate this flexibility, let’s move to &lt;FONT face="Courier New"&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(VS.100).aspx"&gt;ExpandoObject&lt;/A&gt;&lt;/FONT&gt;, which is a part of the new &lt;A href="http://msdn.microsoft.com/en-us/library/dd233052(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd233052(VS.100).aspx"&gt;dynamic language runtime (DLR)&lt;/A&gt;. &lt;FONT face="Courier New"&gt;ExpandoObject &lt;/FONT&gt;instances can add and remove members at run time. Where can you use such an object? XML is a good candidate here. 
&lt;P&gt;Here’s a code example that I took from &lt;A href="http://msdn.microsoft.com/en-us/library/bb387089.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb387089.aspx"&gt;MSDN&lt;/A&gt;. (Yes, I am an MSDN writer myself, so I use MSDN a lot.)&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement &lt;/SPAN&gt;contactXML =
    &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Contact"&lt;/SPAN&gt;,
        &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Name"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Patrick Hines"&lt;/SPAN&gt;),
        &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Phone"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"206-555-0144"&lt;/SPAN&gt;),
        &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Address"&lt;/SPAN&gt;,
            &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Street1"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"123 Main St"&lt;/SPAN&gt;),
            &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"City"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Mercer Island"&lt;/SPAN&gt;),
            &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"State"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"WA"&lt;/SPAN&gt;),
            &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Postal"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"68042"&lt;/SPAN&gt;)
        )
    );&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Although LINQ to XML is a good technology and I really love it, those &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt; parts look a little bit annoying. This is how I can rewrite it by using &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt;.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;dynamic &lt;/SPAN&gt;contact = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpandoObject&lt;/SPAN&gt;();
contact.Name = &lt;SPAN style="COLOR: #a31515"&gt;"Patrick Hines"&lt;/SPAN&gt;;
contact.Phone = &lt;SPAN style="COLOR: #a31515"&gt;"206-555-0144"&lt;/SPAN&gt;;
contact.Address = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpandoObject&lt;/SPAN&gt;();
contact.Address.Street = &lt;SPAN style="COLOR: #a31515"&gt;"123 Main St"&lt;/SPAN&gt;;
contact.Address.City = &lt;SPAN style="COLOR: #a31515"&gt;"Mercer Island"&lt;/SPAN&gt;;
contact.Address.State = &lt;SPAN style="COLOR: #a31515"&gt;"WA"&lt;/SPAN&gt;;
contact.Address.Postal = &lt;SPAN style="COLOR: #a31515"&gt;"68402"&lt;/SPAN&gt;;&lt;/PRE&gt;
&lt;P&gt;Just note a couple of things. First, look at the declaration of &lt;FONT face="Courier New"&gt;contact&lt;/FONT&gt;. 
&lt;P&gt;&lt;SPAN style="COLOR: blue"&gt;dynamic &lt;/SPAN&gt;contact = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpandoObject&lt;/SPAN&gt;(); 
&lt;P&gt;I didn’t write &lt;SPAN style="COLOR: #2b91af"&gt;ExpandoObject &lt;/SPAN&gt;contact = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpandoObject&lt;/SPAN&gt;(), because if I did &lt;FONT face="Courier New"&gt;contact&lt;/FONT&gt; would be a statically-typed object of the &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; type. And of course, statically-typed variables cannot add members at run time. So I used the new &lt;FONT face="Courier New"&gt;dynamic&lt;/FONT&gt; keyword instead of a type declaration, and since &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; supports dynamic operations, the code works. 
&lt;P&gt;Second, notice that every time I needed a node to have subnodes, I simply created a new instance of &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; as a member of the &lt;FONT face="Courier New"&gt;contact&lt;/FONT&gt; object. 
&lt;P&gt;It looks like the &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; example has more code, but it’s actually easier to read. You can clearly see what subnodes each node contains, and you don’t need to deal with the parentheses and indentation. But the best part is how you can access the elements now. 
&lt;P&gt;This is the code you need to print the State field in LINQ to XML. &lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine((&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;)contactXML.Element(&lt;SPAN style="COLOR: #a31515"&gt;"Address"&lt;/SPAN&gt;).Element(&lt;SPAN style="COLOR: #a31515"&gt;"State"&lt;/SPAN&gt;));&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;And this is how it looks with &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt;.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(contact.Address.State);&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;But what if you want to have several Contact nodes? Like in the following LINQ to XML example.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement &lt;/SPAN&gt;contactsXML =
    &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Contacts"&lt;/SPAN&gt;,
        &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Contact"&lt;/SPAN&gt;,
            &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Name"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Patrick Hines"&lt;/SPAN&gt;),
            &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Phone"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"206-555-0144"&lt;/SPAN&gt;)
        ),
        &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Contact"&lt;/SPAN&gt;,
            &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Name"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Ellen Adams"&lt;/SPAN&gt;),
            &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Phone"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"206-555-0155"&lt;/SPAN&gt;)
        )
    );&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Just use a collection of dynamic objects. &lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;dynamic &lt;/SPAN&gt;contacts = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;dynamic&lt;/SPAN&gt;&amp;gt;();

contacts.Add(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpandoObject&lt;/SPAN&gt;());
contacts[0].Name = &lt;SPAN style="COLOR: #a31515"&gt;"Patrick Hines"&lt;/SPAN&gt;;
contacts[0].Phone = &lt;SPAN style="COLOR: #a31515"&gt;"206-555-0144"&lt;/SPAN&gt;;

contacts.Add(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpandoObject&lt;/SPAN&gt;());
contacts[1].Name = &lt;SPAN style="COLOR: #a31515"&gt;"Ellen Adams"&lt;/SPAN&gt;;
contacts[1].Phone = &lt;SPAN style="COLOR: #a31515"&gt;"206-555-0155"&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Technically speaking, I could write &lt;SPAN style="COLOR: blue"&gt;dynamic &lt;/SPAN&gt;contacts = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpandoObject&lt;/SPAN&gt;&amp;gt;() and the example would work. However, there are some situations where this could cause problems, because the actual type of the list elements should be &lt;FONT face="Courier New"&gt;dynamic&lt;/FONT&gt; and not &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt;, and these are two different types. (Once again, references to the &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; objects are statically-typed and do not support dynamic operations.) 
&lt;P&gt;Now, if you want to find all the names in your contact list, just iterate over the collection. &lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;contacts)
    &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(c.Name);&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Again, this syntax is better than LINQ to XML version. &lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;contactsXML.Descendants(&lt;SPAN style="COLOR: #a31515"&gt;"Name"&lt;/SPAN&gt;))
    &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine((&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;)c);&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;So far, so good. But one of the main advantages of LINQ to XML is, well, LINQ. How would you query dynamic objects? Although there is still a lot to be done in this particular area, you can query dynamic objects. For example, let’s find all the phone numbers for the specified name.&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;phones = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;(contacts &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;dynamic&lt;/SPAN&gt;&amp;gt;)
             &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;c.Name == &lt;SPAN style="COLOR: #a31515"&gt;"Patrick Hines"
             &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c.Phone;&lt;/PRE&gt;
&lt;P&gt;True, the cast here doesn’t look like something strictly necessary. Certainly the compiler could have determined at run-time that &lt;FONT face="Courier New"&gt;contacts&lt;/FONT&gt; is &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;dynamic&lt;/SPAN&gt;&amp;gt;. But as I said, there is still some work to be done in this area. 
&lt;P&gt;Another thing to note is that this trick works only for the &lt;A href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb397919.aspx"&gt;LINQ to Objects&lt;/A&gt; provider. To use dynamic objects in LINQ to SQL or other LINQ providers, you need to modify the providers themselves, and that’s a completely different story. 
&lt;P&gt;However, even with the cast, syntax is still better than in a LINQ to XML query. &lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;phonesXML = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;contactsXML.Elements(&lt;SPAN style="COLOR: #a31515"&gt;"Contact"&lt;/SPAN&gt;)
                &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;c.Element(&lt;SPAN style="COLOR: #a31515"&gt;"Name"&lt;/SPAN&gt;).Value == &lt;SPAN style="COLOR: #a31515"&gt;"Patrick Hines"
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c.Element(&lt;SPAN style="COLOR: #a31515"&gt;"Phone"&lt;/SPAN&gt;).Value;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;Sure, there are some things that look better in LINQ to XML. For example, if you want to delete a phone number for all the contacts, you can write just one line of code in LINQ to XML.&lt;/P&gt;&lt;PRE class=code&gt;contactsXML.Elements(&lt;SPAN style="COLOR: #a31515"&gt;"Contact"&lt;/SPAN&gt;).Elements(&lt;SPAN style="COLOR: #a31515"&gt;"Phone"&lt;/SPAN&gt;).Remove();&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Since C# doesn’t have syntax for removing object members, you don’t have an elegant solution here. But &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; implements &lt;SPAN style="COLOR: #2b91af"&gt;IDictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;Object&lt;/SPAN&gt;&amp;gt; to maintain its list of members, and you can delete a member by deleting a key-value pair. &lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;person &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;contacts)
    ((&lt;SPAN style="COLOR: #2b91af"&gt;IDictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;Object&lt;/SPAN&gt;&amp;gt;)person).Remove(&lt;SPAN style="COLOR: #a31515"&gt;"Phone"&lt;/SPAN&gt;);&lt;/PRE&gt;
&lt;P&gt;There are other useful methods in LINQ to XML like &lt;FONT face="Courier New"&gt;Save()&lt;/FONT&gt; and &lt;FONT face="Courier New"&gt;Load()&lt;/FONT&gt;. For &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; you need to write such methods yourself, but probably only once. Here, casting to the &lt;FONT face="Courier New"&gt;IDictionary&lt;/FONT&gt; interface can help as well. 
&lt;P&gt;And although I’ve been comparing LINQ to XML and &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; in this post, these two approaches are not “rivals”. You can convert &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; to &lt;FONT face="Courier New"&gt;XElement&lt;/FONT&gt; and vice versa. For example, this is what the &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; to &lt;FONT face="Courier New"&gt;XElement&lt;/FONT&gt; conversion might look like. &lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;private static &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement &lt;/SPAN&gt;expandoToXML(&lt;SPAN style="COLOR: blue"&gt;dynamic &lt;/SPAN&gt;node, &lt;SPAN style="COLOR: #2b91af"&gt;String &lt;/SPAN&gt;nodeName)
{
    &lt;SPAN style="COLOR: #2b91af"&gt;XElement &lt;/SPAN&gt;xmlNode = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(nodeName);

    &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;property &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;IDictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;Object&lt;/SPAN&gt;&amp;gt;)node)
    {

        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(property.Value.GetType() == &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;ExpandoObject&lt;/SPAN&gt;))
            xmlNode.Add(expandoToXML(property.Value, property.Key));

        &lt;SPAN style="COLOR: blue"&gt;else
            if &lt;/SPAN&gt;(property.Value.GetType() == &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;dynamic&lt;/SPAN&gt;&amp;gt;))
                &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;element &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;dynamic&lt;/SPAN&gt;&amp;gt;)property.Value)
                    xmlNode.Add(expandoToXML(element, property.Key));
            &lt;SPAN style="COLOR: blue"&gt;else
                &lt;/SPAN&gt;xmlNode.Add(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XElement&lt;/SPAN&gt;(property.Key, property.Value));
    }
    &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;xmlNode;
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;This little trick might help you access all the LINQ to XML functions when you need them but at the same time use more convenient syntax when creating and modifying XML trees. 
&lt;P&gt;Of course, XML is not the only area where you can use &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt;. If you heavily use reflection or work a lot with script objects, you can simplify your code with &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt;. On the other hand, &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; is not the only useful class that the DLR provides. The &lt;FONT face="Courier New"&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject(VS.100).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject(VS.100).aspx"&gt;DynamicObject&lt;/A&gt;&lt;/FONT&gt; class, for example, enables you to take more control over dynamic operations and define what actually happens when you access a member or invoke a method. But that’s a topic for another blog post. 
&lt;P&gt;One more thing to note is that libraries that look up members by name might someday adopt the DLR and implement the &lt;FONT face="Courier New"&gt;IDynamicMetaObjectProvider&lt;/FONT&gt; interface. (This interface actually provides all the “magic” – or dynamic dispatch – for &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; and the dynamic feature in general.) For example, if LINQ to XML implements this interface, you would be able to write &lt;SPAN style="COLOR: blue"&gt;dynamic &lt;/SPAN&gt;contacts = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;XmlElement() instead of &lt;SPAN style="COLOR: blue"&gt;dynamic &lt;/SPAN&gt;contacts = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;ExpandoObject() and perform the same operations that I have shown in the examples for the &lt;FONT face="Courier New"&gt;ExpandoObject&lt;/FONT&gt; type. 
&lt;P&gt;All the examples provided in this blog post work in &lt;A href="http://go.microsoft.com/fwlink/?LinkId=151799" mce_href="http://go.microsoft.com/fwlink/?LinkId=151799"&gt;Visual Studio 2010 Beta 1&lt;/A&gt;. If you have any comments or suggestions, you are welcome to post them here or contact the DLR team at &lt;A href="http://www.codeplex.com/dlr" mce_href="http://www.codeplex.com/dlr"&gt;http://www.codeplex.com/dlr&lt;/A&gt;. You can also write an e-mail to the DLR team at &lt;A href="mailto:dlr@microsoft.com" mce_href="mailto:dlr@microsoft.com"&gt;dlr@microsoft.com&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;See how&amp;nbsp;you can&amp;nbsp;improve this example in my next post: &lt;SPAN class=entrylistheader&gt;&lt;A id=bp___v___r___postlist___EntryItems_ctl01_PostTitle href="http://blogs.msdn.com/csharpfaq/archive/2009/10/19/dynamic-in-c-4-0-creating-wrappers-with-dynamicobject.aspx"&gt;&lt;FONT color=#006bad size=2 face="Times New Roman"&gt;Dynamic in C# 4.0: Creating Wrappers with DynamicObject&lt;/FONT&gt;&lt;/A&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9901579" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/Visual+Studio+2010/default.aspx">Visual Studio 2010</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/dynamic+language+runtime/default.aspx">dynamic language runtime</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/DLR/default.aspx">DLR</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/ExpandoObject/default.aspx">ExpandoObject</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category></item><item><title>Generating Dynamic Methods with Expression Trees in Visual Studio 2010</title><link>http://blogs.msdn.com/csharpfaq/archive/2009/09/14/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010.aspx</link><pubDate>Mon, 14 Sep 2009 20:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9895058</guid><dc:creator>Alexandra Rusina</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/9895058.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=9895058</wfw:commentRss><description>&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Expression trees first appeared in Visual Studio 2008, where they were mainly used by LINQ providers. You can use expression trees to represent code in a tree-like format, where each node is an expression. You can also convert expression trees into compiled code and run it. This transformation enables dynamic modification of executable code as well as the execution of LINQ queries in various databases and the creation of dynamic queries. Expression trees in Visual Studio 2008 are explained in Charlie Calvert’s blog post &lt;/FONT&gt;&lt;A href="http://go.microsoft.com/fwlink/?LinkID=153998" mce_href="http://go.microsoft.com/fwlink/?LinkID=153998"&gt;&lt;FONT color=#0000ff size=3 face="Times New Roman"&gt;Expression Tree Basics&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=3 face=Calibri&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;In this post I’m going to show how expression trees were extended in Visual Studio 2010 and how you can use them to generate dynamic methods (a problem that previously could be solved only by emitting MSIL). But although I strongly recommend reading Charlie’s blog post first, I still need to repeat some basics to spell out certain nuances.&lt;/FONT&gt;&lt;/P&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;FONT size=4 face=Cambria&gt;Creating Expression Trees&lt;/FONT&gt;&lt;/H2&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;The easiest way to generate an expression tree is to create an instance of the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Expression&amp;lt;T&amp;gt;&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; type, where &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;T&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; is a delegate type, and assign a lambda expression to this instance. Let’s take a look at the code.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-add-space: auto" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Creating an expression tree by providing a lambda expression.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-bidi-language: AR-SA"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-bidi-language: AR-SA"&gt;&lt;FONT color=#000000&gt;&amp;lt;&lt;/FONT&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Action&lt;/SPAN&gt;&lt;FONT color=#000000&gt;&amp;lt;&lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;FONT color=#000000&gt;&amp;gt;&amp;gt; printExpr = (arg) =&amp;gt; &lt;/FONT&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;&lt;FONT color=#000000&gt;.WriteLine(arg);&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-bidi-language: AR-SA"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-add-space: auto" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Compiling and invoking the expression tree.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-bidi-language: AR-SA"&gt;&lt;FONT color=#000000&gt;printExpr.Compile()(10);&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: auto auto 0pt; mso-add-space: auto" class=MsoNormalCxSpFirst&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: auto auto 0pt; mso-add-space: auto" class=MsoNormalCxSpMiddle&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Prints 10.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: auto auto 0pt; mso-add-space: auto" class=MsoNormalCxSpLast&gt;&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;In this example, the C# compiler generates the expression tree from the provided lambda expression. Note that if you use &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Action&amp;lt;int&amp;gt;&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; instead of &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Expression&amp;lt;Action&amp;lt;int&amp;gt;&amp;gt;&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; as a type of the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;printExpr&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; object, no expression tree will be created, because delegates are not converted into expression trees.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;However, this is not the only way to create an expression tree. You can also use classes and methods from the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;System.LINQ.Expressions&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; namespace. For example, you can create the same expression tree by using the following code.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: auto auto 0pt; mso-add-space: auto" class=MsoNormalCxSpLast&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Creating a parameter for the expression tree.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;ParameterExpression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; param = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Parameter(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;), &lt;SPAN style="COLOR: #a31515"&gt;"arg"&lt;/SPAN&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-add-space: auto" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Creating an expression for the method call and specifying its parameter.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;MethodCallExpression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; methodCall = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Call(&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;typeof&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;).GetMethod(&lt;SPAN style="COLOR: #a31515"&gt;"WriteLine"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt;[] { &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;) }),&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;param&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-add-space: auto" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Compiling and invoking the methodCall expression.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;.Lambda&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Action&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;&amp;gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;methodCall,&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;new&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ParameterExpression&lt;/SPAN&gt;[] { param }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;).Compile()(10);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: auto auto 0pt; mso-add-space: auto" class=MsoNormalCxSpFirst&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Also prints 10.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 6pt 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Of course this looks much more complicated, but this is what actually happens when you supply a lambda expression to an expression tree.&lt;/FONT&gt;&lt;/P&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;FONT size=4 face=Cambria&gt;Expression Trees vs. Lambda Expressions&lt;/FONT&gt;&lt;/H2&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;A common misconception is that expression trees are identical to lambda expressions. This is not true. On the one hand, as I have already shown, you can create and modify expression trees by using API methods, without using lambda expression syntax at all. On the other hand, not every lambda expression can be implicitly converted into an expression tree. For example, multiline lambdas (also called &lt;I style="mso-bidi-font-style: normal"&gt;statement lambdas&lt;/I&gt;) cannot be implicitly converted into expression trees. &lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: auto auto 0pt; mso-add-space: auto" class=MsoNormalCxSpLast&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// You can use multiline lambdas in delegates.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Action&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt; printTwoLines = (arg) =&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Console&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Print arg:"&lt;/SPAN&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Console&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;.WriteLine(arg);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;};&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-add-space: auto" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// But in expression trees this generates a compiler error.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Action&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;&amp;gt; printTwoLinesExpr = (arg) =&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Console&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Print arg:"&lt;/SPAN&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Console&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;.WriteLine(arg);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;};&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;FONT size=4 face=Cambria&gt;Expression Trees in Visual Studio 2010&lt;/FONT&gt;&lt;/H2&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;All the code examples I have shown so far work (or don’t work) the same in both Visual Studio 2008 and Visual Studio 2010. Now let’s move to C# 4.0 and Visual Studio 2010.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;In Visual Studio 2010, the expression trees API was extended and added to the dynamic language runtime (DLR), so that language implementers can emit expression trees rather than MSIL. To support this new goal, control flow and assignment features were added to expression trees: loops, conditional blocks, try-catch blocks, and so on. &lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;There is a catch: You cannot use these new features “an easy way”, by using lambda expressions syntax. You must use the expression trees API. So the last code example in the previous section still generates a compiler error, even in Visual Studio 2010. &lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;But now you have a way to create such an expression tree by using API methods that were not available in Visual Studio 2008. One of these methods is &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Expression.Block&lt;/SPAN&gt;&lt;FONT face=Calibri&gt;, which enables the execution of several expressions sequentially, and this is exactly the method that I need for this example.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: auto auto 0pt; mso-add-space: auto" class=MsoNormalCxSpLast&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Creating a parameter for an expression tree.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;ParameterExpression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt; param = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Parameter(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;), &lt;SPAN style="COLOR: #a31515"&gt;"arg"&lt;/SPAN&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-add-space: auto" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Creating an expression for printing a constant string.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;MethodCallExpression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt; firstMethodCall = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Call(&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt"&gt;typeof&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;).GetMethod(&lt;SPAN style="COLOR: #a31515"&gt;"WriteLine"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt;[] { &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;) }),&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;.Constant(&lt;SPAN style="COLOR: #a31515"&gt;"Print arg:"&lt;/SPAN&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-add-space: auto" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Creating an expression for printing a passed argument.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;MethodCallExpression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt; secondMethodCall = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Call(&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt"&gt;typeof&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;).GetMethod(&lt;SPAN style="COLOR: #a31515"&gt;"WriteLine"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt;[] { &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;) }),&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;param&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-add-space: auto" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Creating a block expression that combines two method calls.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;BlockExpression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt; block = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Block(firstMethodCall, secondMethodCall);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-add-space: auto" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;// Compiling and invoking an expression tree.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;.Lambda&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Action&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;&amp;gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;block,&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt"&gt;new&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ParameterExpression&lt;/SPAN&gt;[] { param }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;).Compile()(10); &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 6pt 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;I’ll repeat this: Although the expression trees API was extended, the way expression trees work with lambda expression syntax did not change. This means that LINQ queries in Visual Studio 2010 have the same features (and the same limitations) that they had in Visual Studio 2008.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;But because of the new features, you can find more areas outside of LINQ where you can use expression trees. &lt;/FONT&gt;&lt;/P&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;FONT size=4 face=Cambria&gt;Generating Dynamic Methods&lt;/FONT&gt;&lt;/H2&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Now let’s move to the real problems where the new API can help. The most well-known one is creating dynamic methods. The common solution to this problem is to use &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;System.Reflection.Emit&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; and work directly with MSIL. Needless to say, the resulting code is hard to write and read. &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Basically, the expression tree that prints two lines to the console that I have shown previously is already an example of a dynamic method. But let’s try a little bit more complex one to demonstrate more features of the new API.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Thanks to John Messerly, a developer on the DLR team, for providing the following example.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Assume that you have a simple method that calculates the factorial of a number.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;static&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; CSharpFact(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; value)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; result = 1;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;while&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; (value &amp;gt; 1)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt 0.5in" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;result *= value--;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;return&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; result;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 6pt 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Now you want a dynamic method that does the same thing. We have several essential elements here: a parameter that is passed to a method, a local variable, and a loop. This is how you can represent these elements by using the expression trees API. &lt;/FONT&gt;&lt;/P&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt"&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt"&gt;static&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt; &lt;/FONT&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&lt;FONT color=#000000&gt;&amp;lt;&lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;FONT color=#000000&gt;, &lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;FONT color=#000000&gt;&amp;gt; ETFact()&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// Creating a parameter expression.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;ParameterExpression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt; value = &lt;/FONT&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&lt;FONT color=#000000&gt;.Parameter(&lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;&lt;FONT color=#000000&gt;(&lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;FONT color=#000000&gt;), &lt;/FONT&gt;&lt;SPAN style="COLOR: #a31515"&gt;"value"&lt;/SPAN&gt;&lt;FONT color=#000000&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// Creating an expression to hold a local variable. &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;ParameterExpression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt; result = &lt;/FONT&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&lt;FONT color=#000000&gt;.Parameter(&lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;&lt;FONT color=#000000&gt;(&lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;FONT color=#000000&gt;), &lt;/FONT&gt;&lt;SPAN style="COLOR: #a31515"&gt;"result"&lt;/SPAN&gt;&lt;FONT color=#000000&gt;);&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// Creating a label to jump to from a loop.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;LabelTarget&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt; label = &lt;/FONT&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&lt;FONT color=#000000&gt;.Label(&lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;&lt;FONT color=#000000&gt;(&lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;FONT color=#000000&gt;));&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// Creating a method body.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;BlockExpression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt; block = &lt;/FONT&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&lt;FONT color=#000000&gt;.Block(&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// Adding a local variable.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt"&gt;new&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;[] { result },&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// Assigning a constant to a local variable: result = 1&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;.Assign(result, &lt;/FONT&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&lt;FONT color=#000000&gt;.Constant(1)),&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// Adding a loop.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;.Loop(&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt 0.5in" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// Adding a conditional block into the loop.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt 0.5in" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;.IfThenElse(&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt 1in" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// Condition: value &amp;gt; 1&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt 1in" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;.GreaterThan(value, &lt;/FONT&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&lt;FONT color=#000000&gt;.Constant(1)),&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt 1in" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// If true: result *= value --&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt 1in" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;.MultiplyAssign(result,&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt 1.5in" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;.PostDecrementAssign(value)),&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt 1in" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// If false, exit from loop and go to a label.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt 1in" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt"&gt;Expression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;.Break(label, result)&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt 0.5in" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;),&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// Label to jump to.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;label&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-INDENT: 0.5in; MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt"&gt;// Compile an expression tree and return a delegate.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt"&gt;return&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt; &lt;/FONT&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&lt;FONT color=#000000&gt;.Lambda&amp;lt;&lt;/FONT&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&lt;FONT color=#000000&gt;&amp;lt;&lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;FONT color=#000000&gt;, &lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&lt;FONT color=#000000&gt;&amp;gt;&amp;gt;(block, value).Compile();&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt"&gt;&lt;FONT color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3 face=Calibri&gt;Yes, this may look more complicated and less clear than the original C# code. But compare it to what you have to write to generate MSIL.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;static&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; Func&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt; ILFact()&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;var method = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DynamicMethod&lt;/SPAN&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;"factorial"&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;, &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;), &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;new&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;[] { &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;) }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;var il = method.GetILGenerator();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;var result = il.DeclareLocal(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;));&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;var startWhile = il.DefineLabel();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;var returnResult = il.DefineLabel();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// result = 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Ldc_I4_1);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Stloc, result);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// if (value &amp;lt;= 1) branch end&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.MarkLabel(startWhile);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Ldarg_0);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Ldc_I4_1);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Ble_S, returnResult);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// result *= (value--)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Ldloc, result);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Ldarg_0);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Dup);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Ldc_I4_1);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Sub);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Starg_S, 0);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Mul);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Stloc, result);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// end while&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Br_S, startWhile);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// return result&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.MarkLabel(returnResult);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Ldloc, result);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;il.Emit(&lt;SPAN style="COLOR: #2b91af"&gt;OpCodes&lt;/SPAN&gt;.Ret);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; (Func&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;)method.CreateDelegate(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(Func&amp;lt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;gt;));&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNoSpacing&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;H2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;FONT size=4 face=Cambria&gt;If You Want to Know More&lt;/FONT&gt;&lt;/H2&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;In Visual Studio 2010, expression trees were developed as a part of the dynamic language runtime, which is also released as an open-source project. You can download the source code and find the specification and documentation for expression trees on the &lt;/FONT&gt;&lt;A href="http://go.microsoft.com/fwlink/?LinkID=141028" mce_href="http://go.microsoft.com/fwlink/?LinkID=141028"&gt;&lt;FONT color=#0000ff size=3 face="Times New Roman"&gt;CodePlex&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=3 face=Calibri&gt; Web site. &lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;A more advanced example of using the new expression trees API is shown in Bart De Smet's blog post &lt;/FONT&gt;&lt;A href="http://go.microsoft.com/fwlink/?LinkId=163853" mce_href="http://go.microsoft.com/fwlink/?LinkId=163853"&gt;&lt;FONT color=#0000ff size=3 face="Times New Roman"&gt;Expression Trees, Take Two – Introducing System.Linq.Expressions v4.0&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=3 face=Calibri&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 10pt" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;And of course, to try the examples, you need to download &lt;/FONT&gt;&lt;A href="http://go.microsoft.com/fwlink/?LinkId=151799" mce_href="http://go.microsoft.com/fwlink/?LinkId=151799"&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Verdana','sans-serif'; COLOR: #0033cc; FONT-SIZE: 9pt; TEXT-DECORATION: none; text-underline: none; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"&gt;Visual Studio 2010 and .NET Framework 4 Beta 1 here&lt;/SPAN&gt;&lt;/A&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;.&lt;SPAN style="mso-no-proof: yes"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9895058" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/Visual+Studio+2010/default.aspx">Visual Studio 2010</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/dynamic+language+runtime/default.aspx">dynamic language runtime</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/expression+trees/default.aspx">expression trees</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/DLR/default.aspx">DLR</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/C_2300_+4.0/default.aspx">C# 4.0</category></item><item><title>How to use LINQ methods to compare objects of custom types</title><link>http://blogs.msdn.com/csharpfaq/archive/2009/03/25/how-to-use-linq-methods-to-compare-objects-of-custom-types.aspx</link><pubDate>Wed, 25 Mar 2009 23:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9508388</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/9508388.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=9508388</wfw:commentRss><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;LINQ provides a convenient syntax and many useful methods for operating with collections of objects. However, to be correctly processed by LINQ comparison methods such as &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Distinct&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; or &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Intersect&lt;/SPAN&gt;&lt;FONT face=Calibri&gt;, a type must satisfy certain requirements.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Let’s take a look at the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Distinct&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; method, which returns all distinct objects from a collection.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; VERTICAL-ALIGN: top; WORD-BREAK: break-all; LINE-HEIGHT: 140%; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: black; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;List&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;int&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: black; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;&amp;gt; numbers = &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;new&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: black; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt; List&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;int&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: black; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;&amp;gt; { 1, 1, 2, 3 };&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; VERTICAL-ALIGN: top; WORD-BREAK: break-all; LINE-HEIGHT: 140%; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;var&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: black; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt; distinctNumbers = numbers.Distinct();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; VERTICAL-ALIGN: top; WORD-BREAK: break-all; LINE-HEIGHT: 140%; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;foreach&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: black; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt; (&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;var&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: black; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt; number &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;in&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: black; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt; distinctNumbers)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; VERTICAL-ALIGN: top; WORD-BREAK: break-all; LINE-HEIGHT: 140%; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: black; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Console.WriteLine(number);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; VERTICAL-ALIGN: top; WORD-BREAK: break-all; LINE-HEIGHT: 140%; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: black; LINE-HEIGHT: 140%; FONT-FAMILY: 'Courier New'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;The output is: &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="COLOR: #00b050"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;1&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="COLOR: #00b050"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;2&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="COLOR: #00b050"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;3&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;But what if you want to use the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Distinct&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; method for a collection of objects of your own type? For example, like this:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;class&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; Digital { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt; Textual { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;class&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Program&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; &lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;st1:place w:st="on"&gt;Main&lt;/st1:place&gt;(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;[] args)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt;&amp;gt; numbers = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt;&amp;gt; { &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt; { Digital = 1, Textual = &lt;SPAN style="COLOR: #a31515"&gt;"one"&lt;/SPAN&gt; }, &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt; { Digital = 1, Textual = &lt;SPAN style="COLOR: #a31515"&gt;"one"&lt;/SPAN&gt; } ,&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt; { Digital = 2, Textual = &lt;SPAN style="COLOR: #a31515"&gt;"two"&lt;/SPAN&gt; } ,&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt; { Digital = 3, Textual = &lt;SPAN style="COLOR: #a31515"&gt;"three"&lt;/SPAN&gt; } ,&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;};&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt; distinctNumbers = numbers.Distinct();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt; number &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; distinctNumbers)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(number.Digital);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;The code compiles, but the output is different:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="COLOR: #00b050"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;1&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="COLOR: #00b050"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;1&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="COLOR: #00b050"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;2&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="COLOR: #00b050"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;3&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Why did that happen? The answer is in the LINQ implementation details. To be correctly processed by the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Distinct&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; method, a type must implement the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IEquatable&amp;lt;T&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: black"&gt;&lt;FONT face=Calibri&gt; interface and provide its own &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Equals&lt;/SPAN&gt;&lt;SPAN style="COLOR: black"&gt;&lt;FONT face=Calibri&gt; and &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;GetHashCode&lt;/SPAN&gt;&lt;SPAN style="COLOR: black"&gt;&lt;FONT face=Calibri&gt; methods.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;SPAN style="COLOR: black"&gt;&lt;FONT face=Calibri&gt;So, the &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Number&lt;/SPAN&gt;&lt;SPAN style="COLOR: black"&gt;&lt;FONT face=Calibri&gt; class from the previous example should actually look like this:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;class&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt;: &lt;SPAN style="COLOR: #2b91af"&gt;IEquatable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt;&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; Digital { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt; Textual { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; Equals(&lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt; other)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// Check whether the compared object is null.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;Object&lt;/SPAN&gt;.ReferenceEquals(other, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)) &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// Check whether the compared object references the same data.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;Object&lt;/SPAN&gt;.ReferenceEquals(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;, other)) &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// Check whether the objects’ properties are equal.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; Digital.Equals(other.Digital) &amp;amp;&amp;amp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Textual.Equals(other.Textual);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// If Equals returns true for a pair of objects,&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// GetHashCode must return the same value for these objects.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; GetHashCode()&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// Get the hash code for the Textual field if it is not null.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; hashTextual = Textual == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt; ? 0 : Textual.GetHashCode();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// Get the hash code for the Digital field.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; hashDigital = Digital.GetHashCode();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;// Calculate the hash code for the object.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; hashDigital ^ hashTextual;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;}&lt;/SPAN&gt;&lt;SPAN style="COLOR: black"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;But what if you cannot modify the type? What if it was provided by a library and you have no way of implementing the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IEquatable&amp;lt;T&amp;gt;&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; interface in this type? The answer is to create your own equality comparer and pass it as a parameter to the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Distinct&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; method. &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;The equality comparer must implement the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IEqualityComparer&amp;lt;T&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: black"&gt;&lt;FONT face=Calibri&gt; interface and, again, provide &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;GetHashCode&lt;/SPAN&gt;&lt;SPAN style="COLOR: black"&gt;&lt;FONT face=Calibri&gt; and &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Equals&lt;/SPAN&gt;&lt;SPAN style="COLOR: black"&gt;&lt;FONT face=Calibri&gt; methods.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;SPAN style="COLOR: black"&gt;&lt;FONT face=Calibri&gt;Here is how the equality comparer for the original &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Number&lt;/SPAN&gt;&lt;SPAN style="COLOR: black"&gt;&lt;FONT face=Calibri&gt; class might look:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;class&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NumberComparer&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IEqualityComparer&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt;&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; Equals(&lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt; x, &lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt; y)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;Object&lt;/SPAN&gt;.ReferenceEquals(x, y)) &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;Object&lt;/SPAN&gt;.ReferenceEquals(x, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) || &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Object&lt;/SPAN&gt;.ReferenceEquals(y, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;))&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; x.Digital == y.Digital &amp;amp;&amp;amp; x.Textual == y.Textual;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; GetHashCode(&lt;SPAN style="COLOR: #2b91af"&gt;Number&lt;/SPAN&gt; number)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;Object&lt;/SPAN&gt;.ReferenceEquals(number, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)) &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; 0;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; hashTextual = number.Textual == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;? 0 : number.Textual.GetHashCode();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; hashDigital = number.Digital.GetHashCode();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; hashTextual ^ hashDigital;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;And don’t forget to pass the comparer to the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Distinct&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; method:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;var&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; distinctNumbers = numbers.Distinct(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NumberComparer&lt;/SPAN&gt;());&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Of course, these rules don't just apply to the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Distinct&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; method. For example, the same is true for the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Contains&lt;/SPAN&gt;&lt;FONT face=Calibri&gt;, &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Except&lt;/SPAN&gt;&lt;FONT face=Calibri&gt;, &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Intersect&lt;/SPAN&gt;&lt;FONT face=Calibri&gt;, and &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Union&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; methods. In general, if you see that a LINQ method has an overload that accepts the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IEqualityComparer&amp;lt;T&amp;gt;&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; parameter, it probably means that to use it for your own data types you need to either implement &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IEquatable&amp;lt;T&amp;gt;&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; in your class or create your own equality comparer.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" minmax_bound="true"&gt;&lt;FONT size=3 minmax_bound="true"&gt;&lt;FONT face=Calibri minmax_bound="true"&gt;&lt;o:p minmax_bound="true"&gt;[author: Alexandra Rusina, Programming Writer]&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9508388" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/LINQ+to+Objects/default.aspx">LINQ to Objects</category></item><item><title>Does the “LINQ to Objects” provider have built-in performance optimization?</title><link>http://blogs.msdn.com/csharpfaq/archive/2009/01/26/does-the-linq-to-objects-provider-have-built-in-performance-optimization.aspx</link><pubDate>Tue, 27 Jan 2009 03:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9376583</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/9376583.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=9376583</wfw:commentRss><description>&lt;P class=MsoListBulletCxSpFirst style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0in; mso-add-space: auto; tab-stops: .5in"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Let’s start with the basics and maybe repeat some information that many of you already know. One of the most important concepts in LINQ performance and optimization is, of course, deferred execution. It simply means that when you declare a variable and assign it a query expression, that expression is not executed immediately. &lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoListBulletCxSpLast style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0in; mso-add-space: auto; tab-stops: .5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;// Query is not executed.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;var&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; query = &lt;SPAN style="COLOR: blue"&gt;from&lt;/SPAN&gt; item &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; storage &lt;SPAN style="COLOR: blue"&gt;select&lt;/SPAN&gt; item;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoListBullet style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0in; mso-add-space: auto; tab-stops: .5in"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face=Calibri size=3&gt;The variable &lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;query&lt;/SPAN&gt;&lt;FONT face=Calibri size=3&gt; now stores the command, and the query execution is deferred until you request the actual data from the query. This usually happens either within a &lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;foreach&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt; loop or when you call an aggregate method such as &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Min&lt;/SPAN&gt;&lt;FONT face=Calibri&gt;, &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Max&lt;/SPAN&gt;&lt;FONT face=Calibri&gt;, and &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Average&lt;/SPAN&gt;&lt;FONT face=Calibri&gt;, or when you cache the query results using the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;ToList&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; or &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;ToArray&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; methods.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;// foreach loop.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;foreach&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; (&lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt; item &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; query)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #2b91af; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Console&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;.WriteLine(item);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;// Count method.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;int&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; total = query.Count();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;// ToArray method.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;var&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; cachedQuery = query.ToArray();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face=Calibri size=3&gt;Now let’s look at what else is happening behind the scenes. Does any compiler-level optimization happen during the query execution? The answer is yes. However, there is a catch. From now on we will talk only about queries for &lt;/FONT&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx"&gt;&lt;SPAN style="COLOR: windowtext; FONT-FAMILY: 'Courier New'; TEXT-DECORATION: none; text-underline: none"&gt;&lt;FONT size=3&gt;IEnumerable&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/A&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;and &lt;/FONT&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" mce_href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx"&gt;&lt;SPAN style="COLOR: windowtext; FONT-FAMILY: 'Courier New'; TEXT-DECORATION: none; text-underline: none"&gt;&lt;FONT size=3&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/A&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&amp;nbsp;collections that use the “LINQ to Objects” LINQ provider. For other LINQ providers, including LINQ to SQL and LINQ to XML, different optimization rules might apply.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;Note:&lt;/B&gt; It is often believed that because of deferred execution it takes longer to execute a query for the first time. However, in the case of LINQ to Objects queries, there is no difference between the first execution and subsequent ones. With other LINQ providers the rules might be different (for example, there might be some caching going on), but you need to refer to the particular provider's documentation for details.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;The LINQ to Objects queries are optimized in the following cases:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"&gt;&lt;SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3&gt;·&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Some method calls are optimized if the data source implements a necessary interface. The following table lists these optimizations.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;TABLE class=MsoNormalTable style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid black .5pt; mso-yfti-tbllook: 160; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-border-insideh: .5pt solid black; mso-border-insidev: .5pt solid black" cellSpacing=0 cellPadding=0 border=1 class="MsoNormalTable"&gt;
&lt;TBODY&gt;
&lt;TR style="mso-yfti-irow: 0; mso-yfti-firstrow: yes"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;LINQ method&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Optimization&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow: 1"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT size=3&gt;Cast&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;If the data source already implements &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; for the given &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;T&lt;/SPAN&gt;&lt;FONT face=Calibri&gt;, when the sequence of data is returned without a cast.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow: 2"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT size=3&gt;Contains&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399 rowSpan=2&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;If the data source implements the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;ICollection&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; or &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;ICollection&amp;lt;T&amp;gt;&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; interface, the corresponding method of the interface is used.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow: 3"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT size=3&gt;Count&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow: 4"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT size=3&gt;ElementAt&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399 rowSpan=8&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;If the data source implements the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IList &lt;/SPAN&gt;&lt;FONT face=Calibri&gt;or &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IList&amp;lt;T&amp;gt;&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; interface, the interface’s &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Count&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; method and indexing operations are used.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow: 5"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT size=3&gt;ElementAtOrDefault&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow: 6"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT size=3&gt;First&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow: 7"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT size=3&gt;FirstOrDefault&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow: 8"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT size=3&gt;Last&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow: 9"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT size=3&gt;LastOrDefault&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow: 10"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT size=3&gt;Single&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow: 11; mso-yfti-lastrow: yes"&gt;
&lt;TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-top-alt: solid black .5pt" vAlign=top width=399&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;&lt;FONT size=3&gt;SingleOrDefault&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"&gt;&lt;SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3&gt;·&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;If there is a sequence of one or more &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Where &lt;/SPAN&gt;&lt;FONT face=Calibri&gt;operators immediately followed by a sequence of one or more &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;Select&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; operators, the query creates a single &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IEnumerable&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; or &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; object and generates no intermediate ones.&lt;BR&gt;Consider the following query: &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;var&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; query = &lt;SPAN style="COLOR: blue"&gt;from&lt;/SPAN&gt; item &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; storage&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt; item.Category = &lt;SPAN style="COLOR: #a31515"&gt;"Food"&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt; item.Price &amp;lt; 100&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;select&lt;/SPAN&gt; item;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;In this case, only one &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IEnumerable&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; object is be generated for the query.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"&gt;&lt;SPAN style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;FONT size=3&gt;·&lt;/FONT&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;If you query an array or a list&lt;/FONT&gt;&lt;FONT face=Calibri&gt;, the enumerator from the &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IEnumerable&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; or &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/SPAN&gt;&lt;FONT face=Calibri&gt; interface is not used in &lt;/FONT&gt;&lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;foreach&lt;/SPAN&gt;&lt;FONT face=Calibri size=3&gt; loops. Instead, a simple &lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;for&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt; loop over the length of the array or list is created behind the scenes, and the elements are accessed directly. &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;FONT face=Calibri size=3&gt;Furthermore, the &lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;where &lt;/SPAN&gt;&lt;FONT face=Calibri size=3&gt;operators are implemented as simple &lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;if&lt;/SPAN&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt; statements, so no intermediate enumerators or enumerable is created.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;FONT face=Calibri size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Once again, other LINQ providers might have their own optimization rules. But the above rules should give you some idea about how LINQ to Objects works.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;o:p&gt;[author: Alexandra Rusina, Programming Writer]&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9376583" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/LINQ+to+Objects/default.aspx">LINQ to Objects</category></item><item><title>How do I send out simple debug messages to help with my debugging?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/10/09/How-do-I-send-out-simple-debug-messages-to-help-with-my-debugging_3F00_.aspx</link><pubDate>Tue, 10 Oct 2006 02:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:810526</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/810526.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=810526</wfw:commentRss><description>&lt;P&gt;Visual Studio offers tons of useful debugging features and allows you to step through your code line-by-line. However, there are times when you don’t want to step through your application, but want to make it output simple text strings with variable values, etc.&lt;/P&gt;
&lt;P&gt;Enter the System.Diagnostics.Debug class and its Write* methods. By using the Debug class, you can output messages similarly to the way the Win32 API function OutputDebugString. However, the beauty of the Debug class is that when you build your application using the default Release configuration in Visual Studio, no code lines are generated for your Debug.Write* class. This means there’s no performance penalty for using the Debug class in release code.&lt;/P&gt;
&lt;P&gt;To use the Debug class, simply add the “&lt;CODE&gt;using System.Diagnostics;&lt;/CODE&gt;” statement to your C# code file, and call Debug.Write:&lt;/P&gt;&lt;PRE&gt;Debug.Write("Hello, Debugger!");&lt;/PRE&gt;
&lt;P&gt;In addition to Write, you have the possibility to call WriteIf, WriteLine and WriteLineIf. For example:&lt;/P&gt;&lt;PRE&gt;bool @this = true;
bool that = false;
Debug.WriteLineIf(@this || that, "A conditional Hello!");&lt;/PRE&gt;
&lt;P&gt;When you are debugging your application under the Visual Studio debugger, all the messages that are sent out by your Write method calls end up in the Output window (View / Output menu command or Ctrl+W,O on the keyboard). However, if you are running your application outside the debugger (say, by starting it from Windows Explorer), you can still view the messages using tools like &lt;A href="http://www.sysinternals.com/Utilities/DebugView.html" mce_href="http://www.sysinternals.com/Utilities/DebugView.html"&gt;DebugView from Sysinternals&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Remember, if you build your application using the default Release configuration, even DebugView won’t show your messages because the Debug.Write* calls are eliminated altogether. You can also control code generation by defining the DEBUG conditional directive.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Tip:&lt;/STRONG&gt; The .NET debugging/tracing architecture also allows you to redirect debugging messages to different destinations, such as text files. See the help topic “Trace Listeners” for more information.&lt;/P&gt;
&lt;P&gt;[author: &lt;A href="http://www.saunalahti.fi/janij/" mce_href="http://www.saunalahti.fi/janij/"&gt;Jani Järvinen&lt;/A&gt;, C# MVP]&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=810526" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/Debugger_2F00_Debugging/default.aspx">Debugger/Debugging</category></item><item><title>How do I calculate a MD5 hash from a string?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/10/09/How-do-I-calculate-a-MD5-hash-from-a-string_3F00_.aspx</link><pubDate>Tue, 10 Oct 2006 02:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:810522</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/810522.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=810522</wfw:commentRss><description>&lt;P&gt;It is a common practice to store passwords in databases using a hash. MD5 (defined in &lt;A href="http://www.ietf.org/rfc/rfc1321.txt" mce_href="http://www.ietf.org/rfc/rfc1321.txt"&gt;RFC 1321&lt;/A&gt;) is a common hash algorithm, and using it from C# is easy.&lt;/P&gt;
&lt;P&gt;Here’s an implementation of a method that converts a string to an MD5 hash, which is a 32-character string of hexadecimal numbers.&lt;/P&gt;&lt;PRE&gt;public string CalculateMD5Hash(string input)
{
    // step 1, calculate MD5 hash from input
    MD5 md5 = System.Security.Cryptography.MD5.Create();
    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
    byte[] hash = md5.ComputeHash(inputBytes);
 
    // step 2, convert byte array to hex string
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i &amp;lt; hash.Length; i++)
    {
        sb.Append(hash[i].ToString("X2"));
    }
    return sb.ToString();
}
&lt;/PRE&gt;
&lt;P&gt;An example call:&lt;/P&gt;&lt;PRE&gt;string hash = CalculateMD5Hash("abcdefghijklmnopqrstuvwxyz");&lt;/PRE&gt;
&lt;P&gt;…returns a string like this:&lt;/P&gt;&lt;PRE&gt;C3FCD3D76192E4007DFB496CCA67E13B&lt;/PRE&gt;
&lt;P&gt;To make the hex string use lower-case letters instead of upper-case, replace the single line inside the &lt;EM&gt;for &lt;/EM&gt;loop with this line:&lt;/P&gt;&lt;PRE&gt;sb.Append(hash[i].ToString("x2"));&lt;/PRE&gt;
&lt;P&gt;The difference is the ToString method parameter.&lt;/P&gt;
&lt;P&gt;[author: &lt;A href="http://www.saunalahti.fi/janij/" mce_href="http://www.saunalahti.fi/janij/"&gt;Jani Järvinen&lt;/A&gt;, C# MVP]&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=810522" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/.NET+Framework/default.aspx">.NET Framework</category></item><item><title>How do I play default Windows sounds?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/27/562557.aspx</link><pubDate>Tue, 28 Mar 2006 05:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:562557</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/562557.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=562557</wfw:commentRss><description>&lt;P&gt;Sometimes, you might want to make your application a bit more audible. If you are using .NET 2.0, you can utilize the new System.Media namespace and its SystemSound and SystemSounds classes.&lt;/P&gt;
&lt;P&gt;The SystemSounds class contains five static properties that you can use to retrieve instances of the SystemSound class. This class in turn contains the Play() method, which you can use to play the wave file associated with the sound in Windows Control Panel. Note that the user can also disable all sounds altogether, which would mean that no sound can be heard through the computer speakers.&lt;/P&gt;
&lt;P&gt;To play for example the classical beep sound, you could use the following code:&lt;/P&gt;&lt;PRE&gt;System.Media.SystemSounds.Beep.Play();&lt;/PRE&gt;
&lt;P&gt;Similarly, you could play the “Question” sound with this code:&lt;/P&gt;&lt;PRE&gt;System.Media.SystemSounds.Question.Play();&lt;/PRE&gt;
&lt;P&gt;The System.Media namespace is defined in System.dll, so there are no new DLLs you would need to add to your project’s references to use the above code.&lt;/P&gt;
&lt;P&gt;[author: &lt;A href="http://www.saunalahti.fi/janij/"&gt;Jani Järvinen&lt;/A&gt;, C# MVP]&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=562557" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/.NET+Framework/default.aspx">.NET Framework</category></item><item><title>How can I easily log a message to a file for debugging purposes?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/27/562555.aspx</link><pubDate>Tue, 28 Mar 2006 04:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:562555</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/562555.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=562555</wfw:commentRss><description>&lt;P&gt;Often, you need a way to monitor your applications once they are running on the server or even at the customer site -- away from your Visual Studio debugger. In those situations, it is often helpful to have a simple routine that you can use to log messages to a text file for later analysis.&lt;/P&gt;
&lt;P&gt;Here’s a simple routine that has helped me a lot for example when writing server applications without an user interface:&lt;/P&gt;&lt;PRE&gt;using System.IO;
        
public string GetTempPath()
{
    string path = System.Environment.GetEnvironmentVariable("TEMP");
    if (!path.EndsWith("\\")) path += "\\";
    return path;
}

public void LogMessageToFile(string msg)
{
    System.IO.StreamWriter sw = System.IO.File.AppendText(
        GetTempPath() + "My Log File.txt");
    try
    {
        string logLine = System.String.Format(
            "{0:G}: {1}.", System.DateTime.Now, msg);
        sw.WriteLine(logLine);
    }
    finally
    {
        sw.Close();
    }
}&lt;/PRE&gt;
&lt;P&gt;With this simple method, all you need to do is to pass in a string like this:&lt;/P&gt;&lt;PRE&gt;LogMessageToFile("Hello, World");&lt;/PRE&gt;
&lt;P&gt;The current date and time are automatically inserted to the log file along with your message.&lt;/P&gt;
&lt;P&gt;[author: &lt;A href="http://www.saunalahti.fi/janij/"&gt;Jani Järvinen&lt;/A&gt;, C# MVP]&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=562555" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/Debugger_2F00_Debugging/default.aspx">Debugger/Debugging</category></item><item><title>How can I speed up hashtable lookups with struct object as keys?</title><link>http://blogs.msdn.com/csharpfaq/archive/2006/03/20/556192.aspx</link><pubDate>Tue, 21 Mar 2006 04:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:556192</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>21</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/556192.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=556192</wfw:commentRss><description>&lt;p&gt;
When you have struct objects as the key in a hashtable, the lookup operation of the hashtable performs miserably. This can be attributes to the GetHashCode() function which is used internally to do the lookup.
&lt;/p&gt;&lt;p&gt;
If a struct contains only simple value types (int, short, etc.), the algorithm which computes the GetHashCode creates hashes which fall into mostly the same bucket.
&lt;/p&gt;&lt;p&gt;
Example, lets say, the hashtable creates 10 buckets. Then, most probably, all the keys are being put into the same bucket. Hence when a lookup is performed, the .NET runtime has to traverse through this entire bucket to get the value.
&lt;/p&gt;&lt;p&gt;&lt;pre&gt;
BUCKET1 - Value1, Value2, Value3,...,valuen
BUCKET2 - (empty)
BUCKET3 - (empty)
BUCKET4 - (empty)
BUCKET5 - (empty)
BUCKET6 - (empty)
BUCKET7 - (empty)
BUCKET8 - (empty)
BUCKET9 - (empty)
BUCKET10- (empty)
&lt;/pre&gt;
&lt;/p&gt;&lt;p&gt;
Hence, instead of the lookup operation being O(1), it becomes O(n) on an average case.
&lt;/p&gt;&lt;p&gt;
To overcome this drawback, consider overriding the GetHashCode() function and making the life easier for the .NET Runtime.
&lt;/p&gt;&lt;p&gt;
An example would be to create a string by merging all your value types in the struct and joining them by using a special character as a demarcator.
&lt;/p&gt;&lt;p&gt;
Since your struct is a lookup criteria, it is sure that all the struct values will be different, and hence the string generated is guaranteed unique.
&lt;/p&gt;&lt;p&gt;
Now the string generated has a method(GetHashCode()), since it is derived from System.Object (like all other objects). Just return the output of this API. A code example will help to understand better.
&lt;/p&gt;&lt;p&gt;
&lt;pre&gt;
struct
{
	int a;
	short b;
	public struct(int _a, short _b)
	{
		a = _a;
		b = _b;
	}
	public override int GetHashCode()
	{
		string hash = a.ToString() + ":" b.ToString();
		return hash.GetHashCode();
	}
}
&lt;/pre&gt;
&lt;/p&gt;&lt;p&gt;
Since you are generating hashcode explicitly which is guaranteed to be unique, it will boost the performance of your lookup.
&lt;/p&gt;&lt;p&gt;
Reference: &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemobjectclassgethashcodetopic.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemobjectclassgethashcodetopic.asp
&lt;/a&gt;
&lt;/p&gt;&lt;p&gt;
[author: &lt;a href="http://msmvps.com/blogs/vipul/"&gt;Vipul Patel&lt;/a&gt;, C# &lt;a href="http://mvp.support.microsoft.com/"&gt;MVP&lt;/a&gt;]
&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=556192" width="1" height="1"&gt;</description></item><item><title>Where can I find design and coding guidelines for .NET?</title><link>http://blogs.msdn.com/csharpfaq/archive/2005/02/21/377598.aspx</link><pubDate>Mon, 21 Feb 2005 21:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:377598</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/377598.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=377598</wfw:commentRss><description>&lt;p&gt;&lt;font face="Arial"&gt;tipu_77 asked "What are microsoft suggested naming conventions in C#?"&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;span id="qd_lblAnswer" style="FONT-SIZE: x-small"&gt;&lt;font face="Arial" size="3"&gt;The .NET Framework Team collects their recommendations at their&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="FONT-SIZE: x-small"&gt;&lt;a href="http://www.gotdotnet.com/team/libraries/"&gt;&lt;font face="Arial" size="3"&gt;GotDotNet community site&lt;/font&gt;&lt;/a&gt;&lt;font face="Arial" size="3"&gt;. &lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="FONT-SIZE: x-small"&gt;&lt;font face="Arial" size="3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: x-small"&gt;&lt;font face="Arial" size="3"&gt;which points to a fairly comprehensive MSDN page&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="FONT-SIZE: x-small"&gt;&lt;font face="Arial" size="3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: x-small"&gt;&lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconnetframeworkdesignguidelines.asp"&gt;&lt;font face="Arial" size="3"&gt;Design Guidelines for Class Library Developers&lt;/font&gt;&lt;/a&gt;&lt;font face="Arial" size="3"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="FONT-SIZE: x-small"&gt;&lt;font face="Arial" size="3"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; which includes the section &lt;/font&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: x-small"&gt;&lt;a href="http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconnamingguidelines.asp"&gt;&lt;font face="Arial" size="3"&gt;Naming Guidelines&lt;/font&gt;&lt;/a&gt;&lt;font face="Arial" size="3"&gt; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="FONT-SIZE: x-small"&gt;&lt;font face="Arial" size="3"&gt;&lt;/font&gt;&amp;nbsp;&lt;/p&gt;&lt;/span&gt; &lt;p&gt;&lt;span style="FONT-SIZE: x-small"&gt;&lt;font face="Arial" size="3"&gt;[Author: SantoshZ]&lt;br /&gt;&lt;/font&gt;&lt;/p&gt;&lt;/span&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=377598" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/C_2300_+Language+and+Compiler/default.aspx">C# Language and Compiler</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/.NET+Framework/default.aspx">.NET Framework</category></item><item><title>What does the /target: command line option do in the C# compiler?</title><link>http://blogs.msdn.com/csharpfaq/archive/2004/12/04/275229.aspx</link><pubDate>Sun, 05 Dec 2004 07:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:275229</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/275229.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=275229</wfw:commentRss><description>&lt;p&gt;&lt;font face="Arial"&gt;All the /target: options except &lt;font face="Courier New" size="4"&gt;module&lt;/font&gt; create .NET assemblies. Depending on the option, the compiler adds metadata for the operating system to use when loading the portable executable (PE) file and for the runtime to use in executing the contained assembly or module.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial"&gt;&lt;font face="Courier New" size="4"&gt;module&lt;/font&gt; creates a module. The metadata in the PE does not include a &lt;em&gt;manifest&lt;/em&gt;. Module/s + manifest make an assembly - the smallest unit of deployment. Without the metadata in the manifest, there is little the runtime can do with a module.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial"&gt;&lt;font face="Courier New" size="4"&gt;library&lt;/font&gt; creates an assembly without an entry point, by setting the &lt;em&gt;EntryPointToken&lt;/em&gt; of the PE's CLR header to 0. If you look at the IL, it does not contain the &lt;font face="Courier New"&gt;&lt;font size="4"&gt;.entrypo&lt;/font&gt;int&lt;/font&gt; clause. The runtime cannot start an application if the assembly does not have an entry point.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial"&gt;&lt;font face="Courier New" size="4"&gt;exe&lt;/font&gt; creates an assembly with an entry point, but sets the &lt;font face="Courier New" size="4"&gt;Subsystem&lt;/font&gt; field of the PE header to 3 (Image runs in the Windows character subsystem - see the _IMAGE_OPTIONAL_HEADER structure in winnt.h). If you ILDASM the PE, you will see this as &lt;font face="Courier New" size="4"&gt;.subsystem 0x0003&lt;/font&gt;. The OS launches this as a &lt;em&gt;console&lt;/em&gt; app.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial"&gt;&lt;font face="Courier New" size="4"&gt;winexe&lt;/font&gt; sets the &lt;font face="Courier New"&gt;&lt;font size="4"&gt;Subsystem&lt;/font&gt; &lt;/font&gt;field to 2. (Image runs in the Windows GUI subsystem). The OS launches this as a &lt;em&gt;GUI&lt;/em&gt; app.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial"&gt;[Author: SantoshZ]&lt;br /&gt;See also:&lt;br /&gt;Chapter 3 of &lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/0735615470/002-9375079-6540809?v=glance"&gt;Inside Microsoft .NET IL assembler &lt;/a&gt;(Serge Lidin, Microsoft Press)&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconmetadatapefilestructure.asp"&gt;Metadata and the PE File Structure &lt;/a&gt;at MSDN&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/msdnmag/issues/02/02/PE/default.aspx"&gt;An In-Depth Look into the Win32 Portable Executable File Format &lt;/a&gt;- Part 1 and &lt;a href="http://msdn.microsoft.com/msdnmag/issues/02/03/PE2/default.aspx"&gt;Part 2 &lt;/a&gt;(Matt Pietrek, MSDN Magazine) &lt;br /&gt;&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=275229" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/C_2300_+Language+and+Compiler/default.aspx">C# Language and Compiler</category></item><item><title>What is the difference between const and static readonly?</title><link>http://blogs.msdn.com/csharpfaq/archive/2004/12/03/274791.aspx</link><pubDate>Sat, 04 Dec 2004 01:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:274791</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/274791.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=274791</wfw:commentRss><description>&lt;p&gt;&lt;font face="Arial"&gt;The difference is that the value of a &lt;font face="Courier New"&gt;static readonly&lt;/font&gt; field is set at run time, and can thus be modified by the containing class, w&lt;/font&gt;&lt;font face="Arial"&gt;hereas the value of a &lt;font face="Courier New"&gt;const&lt;/font&gt; field is set to a compile time constant. &lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial"&gt;In &lt;/font&gt;&lt;font face="Arial"&gt;the &lt;font face="Courier New"&gt;static readonly &lt;/font&gt;case, the containing class is allowed to modify it only&lt;/font&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;font face="Arial"&gt;in the variable declaration (through a variable initializer)&lt;/font&gt;&lt;/li&gt; &lt;li&gt;&lt;font face="Arial"&gt;in the static constructor (instance constructors, if it's not static) &lt;/font&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;font face="Arial"&gt;&lt;font face="Courier New"&gt;static readonly &lt;/font&gt;is typically used if the type of the field is not allowed in a &lt;font face="Courier New"&gt;const&lt;/font&gt; declaration, or when the value is not known at compile time. &lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial"&gt;Instance &lt;font face="Courier New"&gt;readonly&lt;/font&gt; fields are also allowed.&amp;nbsp; &lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Arial"&gt;Remember that for reference types, in both cases (static and instance)&amp;nbsp;the &lt;font face="Courier New"&gt;readonly&lt;/font&gt; modifier only prevents you from assigning a new reference to the field.&amp;nbsp; It specifically does not make immutable the object pointed to by the reference.&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;b&gt;&lt;span style="COLOR: navy"&gt;Program&lt;/span&gt;&lt;/b&gt;&lt;span style="COLOR: navy"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;readonly&lt;/span&gt; &lt;b&gt;&lt;span style="COLOR: navy"&gt;Test&lt;/span&gt;&lt;/b&gt; test = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;b&gt;&lt;span style="COLOR: navy"&gt;Test&lt;/span&gt;&lt;/b&gt;();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; &lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;st1:place w:st="on"&gt;Main&lt;/st1:place&gt;(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;[] args)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;test.Name = "Program";&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;test = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;b&gt;&lt;span style="COLOR: navy"&gt;Test&lt;/span&gt;&lt;/b&gt;();&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: green"&gt;// &lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;Error:&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;b&gt;&lt;span style="COLOR: navy"&gt;Test&lt;/span&gt;&lt;/b&gt;&lt;span style="COLOR: navy"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; Name;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;font face="Times New Roman"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt; &lt;p&gt;On the other hand, if Test were a value type, then assignment to test.Name would be an error.&lt;br /&gt;&lt;/p&gt;&lt;/font&gt; &lt;p&gt;&lt;font face="Arial"&gt;&amp;nbsp;&lt;/p&gt;&lt;/font&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=274791" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/C_2300_+Language+and+Compiler/default.aspx">C# Language and Compiler</category></item><item><title>How do I create a constant that is an array?</title><link>http://blogs.msdn.com/csharpfaq/archive/2004/12/03/274417.aspx</link><pubDate>Fri, 03 Dec 2004 20:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:274417</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/274417.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=274417</wfw:commentRss><description>&lt;p&gt;
Strictly speaking you can't, since const can only be applied to a field or local whose value is known at compile time.
&lt;/p&gt;&lt;p&gt;
In both the lines below, the right-hand is not a constant expression (not in C#).
&lt;/p&gt;
&lt;pre
&gt;const int [] constIntArray = newint [] {2, 3, 4};
 // error CS0133: The expression being assigned to 'constIntArray' must be constant
const int [] constIntArrayAnother = {2, 3, 4};
 // error CS0623: Array initializers can only be used in a variable or field
 //               initializer. Try using a new expression instead. 
&lt;/pre&gt;
&lt;p&gt;
However, there are some workarounds, depending on what it is you want to achieve.
&lt;/p&gt;&lt;p&gt;
If want a proper .NET array (System.Array) that cannot be reassigned, then static readonly will do for you. 
&lt;/p&gt;
&lt;pre&gt;
static readonly int [] constIntArray = new int[] {1, 2, 3};
&lt;/pre&gt;
&lt;/p&gt;
The constIntArray field will be initialized before it its first use.
&lt;/p&gt;&lt;p&gt;
If, on the other hand, you really need a const set of values (say as an argument to an attribute constructor), then - if you can limit yourself to integral types - an enum would serve you well.
&lt;/p&gt;&lt;p&gt;
For example:
&lt;/p&gt;
&lt;pre&gt;
[Flags]
public enum Role
{
	Administrator = 1,
	BackupOperator = 2,
	// etc. 
}

public class RoleAttribute : Attribute
{
	public RoleAttribute()
	{
		CreateRole = DefaultRole;
	}

	public RoleAttribute(Role role)
	{
		CreateRole = role;
	}

	public Role CreateRole
	{
		get { return this.createRole; }
		set { this.createRole = value; }
	}

	private Role createRole = 0;
	public const Role DefaultRole = Role.Administrator
	 | Role.BackupOperator;
}

[RoleAttribute(RoleAttribute.DefaultRole)]
public class DatabaseAccount
{
	//.............. 
}
&lt;/pre&gt;
&lt;p&gt;
RoleAttribute, instead of taking an array, would only take a single argument of flags (appropriately or-ed). If the underlying type of the Role enum is long or ulong, that gives you 64 different Roles.
&lt;/p&gt;&lt;p&gt;
[Author: SantoshZ]
&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=274417" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/C_2300_+Language+and+Compiler/default.aspx">C# Language and Compiler</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/General/default.aspx">General</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/Tips/default.aspx">Tips</category></item></channel></rss>