


      <rss version="2.0">
         <channel>
            <title><![CDATA[Vinay Rao | C#]]></title>
            <link>http://www.simplyvinay.com/</link>
            <description>My Personal Website</description>
            <copyright>Copyright 2005 by Vinay Rao</copyright>
   
      <item>
         <title><![CDATA[Structural Design Patterns]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>In the previous few posts I have been writing about design patterns and for now I have completed the structural patterns as described by the GOF. Below are the links to all the structural patterns.</p>  <ol>   <li><a href="http://www.simplyvinay.com/Post/14/Design-Patterns-for-Dummies.-The-Decorator-Pattern.aspx">Decorator Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/15/Design-Patterns-for-Dummies.-The-Proxy-Pattern.aspx">Proxy Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/16/Design-Patterns-for-Dummies.-The-Bridge-pattern.aspx">Bridge Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/17/Design-Patterns-for-Dummies.-The-Composite-Pattern.aspx">Composite Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/18/Design-Patterns-for-Dummies.-The-Flyweight-Pattern.aspx">Flyweight Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/19/Design-Patterns-for-Dummies.-The-Adapter-Pattern.aspx">Adapter Pattern</a> </li>    <li><a href="http://www.simplyvinay.com/Post/21/Design-Patterns-for-Dummies.-The-Facade-Pattern.aspx">Façade Pattern</a> </li> </ol>  <p>In the following posts to come, I will write about the Creational Patterns.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/23/Structural-Design-Patterns.aspx]]></link>
         <pubDate>Mon, 15 Dec 2008 06:00:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Cloning Extension Method]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>As you know cloning creates a replica of the instance of a type. There are basically 2 ways of cloning. <em>Shallow Copy </em>which means the original object and the copied object reference the same memory location after the process and <em>Deep Copy </em>which means the original object and the copied object do not reference the same location after the process. You can shallow copy the object by using the protected <em>MemberwiseClone</em> method of the object. You can implement the <em>Clone</em> method of the <em>ICloneable</em> interface for each class you have to clone for the deep copy. I somehow did not like the idea of implementing this <em>Clone</em> method in each and every class that I wanted to clone, so I created a generic class&#160; to get the deep cloning working. Here is the code.</p>  <pre class="code"><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 GenericClone\par ??\cf0 \{\par ??    \cf1 public\cf0  T DeepClone&lt;T&gt;() \cf1 where\cf0  T : \cf1 class\par ??\cf0     \{\par ??        \cf1 object\cf0  cloned = \cf1 new\cf0  \cf1 object\cf0 ();\par ??        \cf4 BinaryFormatter\cf0  formatter = \cf1 new\cf0  \cf4 BinaryFormatter\cf0 ();\par ??        \cf1 using\cf0  (\cf4 MemoryStream\cf0  stream = \cf1 new\cf0  \cf4 MemoryStream\cf0 ())\par ??        \{\par ??            formatter.Serialize(stream, \cf1 this\cf0 );\par ??            stream.Flush();\par ??            stream.Position = 0;\par ??            cloned = formatter.Deserialize(stream);\par ??        \}\par ??\par ??        \cf1 return\cf0  cloned \cf1 as\cf0  T;\par ??    \}\par ??\}\par ??\par ??\cf1 class\cf0  \cf4 Test\par ??\cf0 \{\par ??    \cf4 GenericClone\cf0  getClone = \cf1 new\cf0  \cf4 GenericClone\cf0 ();\par ??    \cf1 public\cf0  \cf1 void\cf0  Clone()\par ??    \{\par ??        \cf4 Test\cf0  deeptest = getClone.DeepClone&lt;\cf4 Test\cf0 &gt;();\par ??    \}\par ??\}}
-->
<div style="font-size: 10pt; background: white; color: black; font-family: consolas"><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 1</span>&#160;<span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">GenericClone</span></p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> T DeepClone&lt;T&gt;() <span style="color: blue">where</span> T : <span style="color: blue">class</span></p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">object</span> cloned = <span style="color: blue">new</span> <span style="color: blue">object</span>();</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">BinaryFormatter</span> formatter = <span style="color: blue">new</span> <span style="color: #2b91af">BinaryFormatter</span>();</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">using</span> (<span style="color: #2b91af">MemoryStream</span> stream = <span style="color: blue">new</span> <span style="color: #2b91af">MemoryStream</span>())</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; formatter.Serialize(stream, <span style="color: blue">this</span>);</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; stream.Flush();</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; stream.Position = 0;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cloned = formatter.Deserialize(stream);</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 14</span>&#160;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> cloned <span style="color: blue">as</span> T;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 16</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 17</span> }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 18</span>&#160;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 19</span>&#160;<span style="color: blue">class</span> <span style="color: #2b91af">Test</span></p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 20</span> {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 21</span>&#160;&#160;&#160;&#160; <span style="color: #2b91af">GenericClone</span> getClone = <span style="color: blue">new</span> <span style="color: #2b91af">GenericClone</span>();</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 22</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Clone()</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 23</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Test</span> deeptest = getClone.DeepClone&lt;<span style="color: #2b91af">Test</span>&gt;();</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 25</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 26</span> }</p></div></pre>

<p>Thing to note here is that the class which needs to be cloned has to be marked as <em>Serializable </em>for this method to work. To create a clone of a class we just have to create an object of the GenericClone class and call the DeepClone method as in the Test class. The problem here is that we need to have an object of the GenericClone class before we can clone the object. With C# 3.0, now we can write an extension method for the cloning process and call it on the object itself. Here is an example.</p>

<pre class="code"><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 static\cf0  \cf1 class\cf0  \cf4 ExtensionClone\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 static\cf0  T DeepClone&lt;T&gt;(\cf1 this\cf0  \cf1 object\cf0  obj) \cf1 where\cf0  T : \cf1 class\par ??\cf0     \{\par ??        \cf1 object\cf0  cloned = \cf1 new\cf0  \cf1 object\cf0 ();\par ??        \cf4 BinaryFormatter\cf0  formatter = \cf1 new\cf0  \cf4 BinaryFormatter\cf0 ();\par ??        \cf1 using\cf0  (\cf4 MemoryStream\cf0  stream = \cf1 new\cf0  \cf4 MemoryStream\cf0 ())\par ??        \{\par ??            formatter.Serialize(stream, obj);\par ??            stream.Flush();\par ??            stream.Position = 0;\par ??            cloned = formatter.Deserialize(stream);\par ??        \}\par ??\par ??        \cf1 return\cf0  cloned \cf1 as\cf0  T;\par ??    \}\par ??\}\par ??\par ??\cf1 class\cf0  \cf4 Test\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 void\cf0  Get()\par ??    \{\par ??        \cf4 Test\cf0  cloned = \cf1 this\cf0 .DeepClone&lt;\cf4 Test\cf0 &gt;();\par ??    \}\par ??\}}
-->
<div style="font-size: 10pt; background: white; color: black; font-family: consolas"><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 1</span>&#160;<span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">class</span> <span style="color: #2b91af">ExtensionClone</span></p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">static</span> T DeepClone&lt;T&gt;(<span style="color: blue">this</span> <span style="color: blue">object</span> obj) <span style="color: blue">where</span> T : <span style="color: blue">class</span></p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">object</span> cloned = <span style="color: blue">new</span> <span style="color: blue">object</span>();</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">BinaryFormatter</span> formatter = <span style="color: blue">new</span> <span style="color: #2b91af">BinaryFormatter</span>();</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">using</span> (<span style="color: #2b91af">MemoryStream</span> stream = <span style="color: blue">new</span> <span style="color: #2b91af">MemoryStream</span>())</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; formatter.Serialize(stream, obj);</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; stream.Flush();</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; stream.Position = 0;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cloned = formatter.Deserialize(stream);</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 14</span>&#160;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> cloned <span style="color: blue">as</span> T;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 16</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 17</span> }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 18</span>&#160;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 19</span>&#160;<span style="color: blue">class</span> <span style="color: #2b91af">Test</span></p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 20</span> {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 21</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Get()</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 22</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Test</span> cloned = <span style="color: blue">this</span>.DeepClone&lt;<span style="color: #2b91af">Test</span>&gt;();</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 24</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 25</span> }</p></div></pre>

<p>As you can see this is a much cleaner way of cloning the object. I don't have to create an object of the ExtensionClone class to get my cloned object.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/11/Cloning-Extension-Method.aspx]]></link>
         <pubDate>Mon, 20 Oct 2008 15:09:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Extension Methods and LINQ]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>This is the final post about the new features in C# 3.0. You can read the first two posts <a href="http://www.simplyvinay.com/Post/6/Automatic-Properties-and-Object-Initializers.aspx">here</a> and <a href="http://www.simplyvinay.com/Post/8/Type-Inference,-Anonymous-types-and-Lambda-Expressions.aspx" target="_blank">here</a>. In these posts I have just described the new features of C# 3.0 without going much into details. If time permits I will elaborate on each of these.</p>  <p><strong>Extension Methods </strong></p>  <p>Prior to C# 3.0, the only way to update member information of a compiled type, was to recode and recompile the code. But now with extension methods we can allow compiled type to obtain new functionality. This is immensely helpful&#160; when you want to add functionality to a third party type where you are not allowed to change the code. When you create an extension method, it actually behave as if it was part of the original type. Lets see an example.</p>  <pre class="code"><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;\red0\green128\blue0;}??\fs20 \cf1 class\cf0  \cf4 Extensions\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 void\cf0  DoSometing()\par ??    \{\par ??        \cf1 string\cf0  originalString = \cf5 "EXTENSIONS"\cf0 ;\par ??        \cf1 string\cf0  reversedString = originalString.Reverse();\par ??\par ??        \cf4 Console\cf0 .WriteLine(originalString); \cf6 //EXTENSIONS\par ??\cf0         \cf4 Console\cf0 .WriteLine(reversedString); \cf6 //SNOISNETXE\par ??\cf0     \}\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 static\cf0  \cf1 class\cf0  \cf4 ExtensionClass\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 static\cf0  \cf1 string\cf0  Reverse(\cf1 this\cf0  \cf1 string\cf0  str)\par ??    \{\par ??        \cf1 char\cf0 [] chr = str.ToCharArray();\par ??        \cf4 Array\cf0 .Reverse(chr);\par ??        \cf1 return\cf0  \cf1 new\cf0  \cf1 string\cf0 (chr);\par ??    \}\par ??\}}
-->
<div style="font-size: 10pt; background: white; color: black; font-family: consolas"><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 1</span>&#160;<span style="color: blue">class</span> <span style="color: #2b91af">Extensions</span></p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> DoSometing()</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">string</span> originalString = <span style="color: #a31515">&quot;EXTENSIONS&quot;</span>;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">string</span> reversedString = originalString.Reverse();</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 7</span>&#160;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine(originalString); <span style="color: green">//EXTENSIONS</span></p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine(reversedString); <span style="color: green">//SNOISNETXE</span></p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 10</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 11</span> }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 12</span>&#160;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 13</span>&#160;<span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">class</span> <span style="color: #2b91af">ExtensionClass</span></p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 14</span> {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 15</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">string</span> Reverse(<span style="color: blue">this</span> <span style="color: blue">string</span> str)</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 16</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">char</span>[] chr = str.ToCharArray();</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Array</span>.Reverse(chr);</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: blue">string</span>(chr);</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 20</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 21</span> }</p></div></pre>
You create an extension method by creating a static class with a static method. As you can see in the <em>ExtensionClass</em> the method Reverse is taking a string parameter with <em>this</em> keyword which indicates the the method is accessible to type of string. We are using the reverse method in the DoSomething method of the Extensions class. As seen we are not creating an object the <em>ExtensionClass</em> , instead we are calling the Reverse method on the string itself. 

<p><strong>LINQ</strong></p>

<p>Language INtegrated Query(LINQ) enables you to write queries against objects. You can query against any object that implements the <em>IEnumerable&lt;T&gt;</em> interface. All queries are converted into method calls to the <em>Enumerable</em> class. This class contains extension methods that can be applied to any object that implements <em>IEnumerable</em>. Lets see an example.</p>

<pre class="code"><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Consolas;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 class\cf0  \cf4 LINQ\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 void\cf0  SelectFromList()\par ??    \{\par ??        \cf1 var\cf0  list = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf1 string\cf0 &gt; \{ \cf5 "a"\cf0 , \cf5 "b"\cf0 , \cf5 "c"\cf0 , \cf5 "d"\cf0 , \cf5 "e"\cf0  \};\par ??\par ??        \cf1 var\cf0  str = \cf1 from\cf0  s \cf1 in\cf0  list\par ??                  \cf1 where\cf0  s.Contains(\cf5 "a"\cf0 )\par ??                  \cf1 select\cf0  s;\par ??\par ??        \cf1 var\cf0  str1 = list.Where(s =&gt; s.Contains(\cf5 "b"\cf0 )).Select(s =&gt; s);\par ??\par ??    \}\par ??\}}
-->
<div style="font-size: 10pt; background: white; color: black; font-family: consolas"><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 1</span>&#160;<span style="color: blue">class</span> <span style="color: #2b91af">LINQ</span></p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 2</span> {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> SelectFromList()</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> list = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt; { <span style="color: #a31515">&quot;a&quot;</span>, <span style="color: #a31515">&quot;b&quot;</span>, <span style="color: #a31515">&quot;c&quot;</span>, <span style="color: #a31515">&quot;d&quot;</span>, <span style="color: #a31515">&quot;e&quot;</span> };</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 6</span>&#160;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> str = <span style="color: blue">from</span> s <span style="color: blue">in</span> list</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">where</span> s.Contains(<span style="color: #a31515">&quot;a&quot;</span>)</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">select</span> s;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 10</span>&#160;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> str1 = list.Where(s =&gt; s.Contains(<span style="color: #a31515">&quot;a&quot;</span>)).Select(s =&gt; s);</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 12</span>&#160;</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 13</span>&#160;&#160;&#160;&#160; }</p><p style="margin: 0px"><span style="color: #2b91af">&#160;&#160; 14</span> }</p></div></pre>

<p>Here the first LINQ statement is using a query syntax and the second is using a method syntax. The method syntax uses a lambda expression with <em>Where</em> and <em>Select</em> extension methods. These two LINQ statements are the same and gives out the same result. </p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/10/Extension-Methods-and-LINQ.aspx]]></link>
         <pubDate>Wed, 15 Oct 2008 14:20:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Type Inference, Anonymous types and Lambda Expressions]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<p>This is the second of posts about the new features in C# 3.0. You can read the first post <a href="/Post/6/Automatic-Properties-and-Object-Initializers.aspx" target="_blank">here</a>. Well this was long overdue, but anyway, here it is.</p>  <p><strong>Type Inference</strong></p>  <p>When we are using type inference which was introduced in C# 3.0, the compiler determines the type of the variable at compile time. Here is an example of type inference.</p>  <pre class="code"><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;\red0\green128\blue0;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 TypeInference\par ??\cf0 \{\par ??    var msg = \cf5 "Hello C# 3.0"\cf0 ; \cf6 //Compile time error.\par ??\cf0                               \cf6 //The contextual keyword 'var' may \par ??\cf0                               \cf6 //only appear within a local variable declaration\par ??\par ??\cf0     \cf1 public\cf0  \cf1 void\cf0  TypeInferenceMethod()\par ??    \{\par ??        \cf1 var\cf0  msg = \cf5 "Hello C# 3.0"\cf0 ; \cf6 //string\par ??\cf0         \cf1 var\cf0  offset = 0; \cf6 // int\par ??\par ??\cf0         \cf1 var\cf0  temp; \cf6 //Compile time error.\par ??\cf0                   \cf6 //Implicitly-typed local variables must be initialized\par ??\cf0         temp = \cf5 "Error"\cf0 ;\par ??    \}\par ??\}}
-->
<div style="background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: courier new;"><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 1</span>&nbsp;<span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">TypeInference</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 2</span> {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 3</span>&nbsp;&nbsp;&nbsp;&nbsp; var globalStr = <span style="color: rgb(163, 21, 21);">"Hello C# 3.0"</span>; <span style="color: green;">//Compile time error.</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 4</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//The contextual keyword 'var' may </span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 5</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//only appear within a local variable declaration</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 6</span>&nbsp;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 7</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> TypeInferenceMethod()</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 8</span>&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 9</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> msg = <span style="color: rgb(163, 21, 21);">"Hello C# 3.0"</span>; <span style="color: green;">//string</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> offset = 0; <span style="color: green;">// int</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 11</span>&nbsp;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 12</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> temp; <span style="color: green;">//Compile time error.</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 13</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//Implicitly-typed local variables must be initialized</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 14</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temp = <span style="color: rgb(163, 21, 21);">"Error"</span>;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 15</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 16</span> }</p></div></pre>

<p>Here the variable msg will be of type string and offset will be of type int. You have to note that these variables have to be initialized and can only be used as local variables or else you will get a compile time error as is the case with globalStr and temp.</p>

<p><strong>Anonymous types</strong>&nbsp;</p>

<p>Anonymous types are useful when we need to create an object of a class and initialize it without having a concrete class. Let me explain this with an example.</p>

<pre class="code"><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 AnonymousType\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 void\cf0  AnonymousTypeMethod()\par ??    \{\par ??        \cf1 var\cf0  product = \cf1 new\cf0  \{ Id = 1, Name = \cf5 "Product1"\cf0  \};\par ??    \}\par ??\}}
-->
<div style="background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: courier new;"><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 1</span>&nbsp;<span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">AnonymousType</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 2</span> {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 3</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> AnonymousTypeMethod()</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 4</span>&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 5</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> product = <span style="color: blue;">new</span> { Id = 1, Name = <span style="color: rgb(163, 21, 21);">"Product1"</span> };</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 6</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 7</span> }</p></div></pre>

<p>Here we create a type product with properties Id an Name. The good thing about this code is that there is no Product class present. The variable product does have a type though but we don't know its name. We just get a class with properties Id and Name already initialized.&nbsp; </p>

<p><strong>Lambda Expressions</strong></p>

<p>Lambda expressions are a refined way of defining your methods. Anonymous methods were introduced in C# 2.0 which helped us to declare a method inline which was much simpler than the earlier version ( will show an example shortly ). Then came lambdas in C# 3.0 which took anonymous methods to a different level. Lets look at an example of all 3 forms.</p>

<pre class="code"><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red0\green128\blue0;}??\fs20 \cf1 class\cf0  \cf4 Lambdas\par ??\cf0 \{\par ??    \cf5 //1. Using delegates\par ??\cf0     \cf1 static\cf0  \cf1 void\cf0  UsingDelegate()\par ??    \{\par ??        \cf4 List\cf0 &lt;\cf1 int\cf0 &gt; intList = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf1 int\cf0 &gt; \{ 1, 2, 4, 5, 6 \};\par ??        \cf4 Predicate\cf0 &lt;\cf1 int\cf0 &gt; callback = \cf1 new\cf0  \cf4 Predicate\cf0 &lt;\cf1 int\cf0 &gt;(IsEvenNumber);\par ??        \cf4 List\cf0 &lt;\cf1 int\cf0 &gt; evenNumbers = intList.FindAll(callback);\par ??    \}\par ??    \par ??    \cf1 static\cf0  \cf1 bool\cf0  IsEvenNumber(\cf1 int\cf0  i)\par ??    \{\par ??        \cf1 return\cf0  (i % 2) == 0;\par ??    \}\par ??\par ??    \cf5 //2. Using anonymous methods\par ??\cf0     \cf1 static\cf0  \cf1 void\cf0  UsingAnonymousMethod()\par ??    \{\par ??        \cf4 List\cf0 &lt;\cf1 int\cf0 &gt; intList = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf1 int\cf0 &gt; \{ 1, 2, 4, 5, 6 \};\par ??        \cf4 List\cf0 &lt;\cf1 int\cf0 &gt; evenNumbers =\par ??            intList.FindAll(\par ??                    \cf1 delegate\cf0 (\cf1 int\cf0  i)\par ??                    \{\par ??                        \cf1 return\cf0  (i % 2) == 0;\par ??                    \}\par ??                );\par ??    \}\par ??\par ??    \cf5 //3. Using lambdas\par ??\cf0     \cf1 static\cf0  \cf1 void\cf0  UsingLambdas()\par ??    \{\par ??        \cf4 List\cf0 &lt;\cf1 int\cf0 &gt; intList = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf1 int\cf0 &gt; \{ 1, 2, 4, 5, 6 \};\par ??        \cf4 List\cf0 &lt;\cf1 int\cf0 &gt; evenNumbers = intList.FindAll(i =&gt; (i % 2) == 0);\par ??    \}\par ??\}}
-->
<div style="background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: courier new;"><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 1</span>&nbsp;<span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Lambdas</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 2</span> {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 3</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//1. Using delegates</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 4</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">static</span> <span style="color: blue;">void</span> UsingDelegate()</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 5</span>&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 6</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">int</span>&gt; intList = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">int</span>&gt; { 1, 2, 4, 5, 6 };</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Predicate</span>&lt;<span style="color: blue;">int</span>&gt; getEvenNumbers = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Predicate</span>&lt;<span style="color: blue;">int</span>&gt;(IsEven);</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">int</span>&gt; evenNumbers = intList.FindAll(getEvenNumbers);</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp; 9</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 10</span>&nbsp;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 11</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">static</span> <span style="color: blue;">bool</span> IsEven(<span style="color: blue;">int</span> i)</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 12</span>&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 13</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> (i % 2) == 0;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 14</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 15</span>&nbsp;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 16</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//2. Using anonymous methods</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 17</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">static</span> <span style="color: blue;">void</span> UsingAnonymousMethod()</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 18</span>&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">int</span>&gt; intList = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">int</span>&gt; { 1, 2, 4, 5, 6 };</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 20</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">int</span>&gt; evenNumbers =</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; intList.FindAll(</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 22</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">delegate</span>(<span style="color: blue;">int</span> i)</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 23</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 24</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> (i % 2) == 0;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 25</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 26</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 27</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 28</span>&nbsp;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 29</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//3. Using lambdas</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 30</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">static</span> <span style="color: blue;">void</span> UsingLambdas()</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 31</span>&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 32</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">int</span>&gt; intList = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">int</span>&gt; { 1, 2, 4, 5, 6 };</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 33</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">List</span>&lt;<span style="color: blue;">int</span>&gt; evenNumbers = intList.FindAll(i =&gt; (i % 2) == 0);</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 34</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp; 35</span> }</p></div></pre>

<p>Here I have written 3 methods which basically creates an integer list and finds out all even numbers in it. The first method is done using the traditional delegate pattern. Here we create a predicate pointing to a method which checks for a number to be even. Then we call the FindAll method for the list and pass this predicate to get the list of even numbers. As you can see the IsEven method might not be used outside the scope of this method. To solve this issue C# introduced anonymous methods. In anonymous methods we can declare the method inline as can be seen the example. This saves us from writing a different method. When you look at the third method we see that a single line of code is doing all the work. In fact&nbsp; i =&gt; (i % 2) == 0 is all that is needed to do the functionality that our predicate or delegate did. Lambda expression are internally converted into anonymous delegates. Just a quicker and neat way of doing things.</p>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/8/Type-Inference,-Anonymous-types-and-Lambda-Expressions.aspx]]></link>
         <pubDate>Wed, 01 Oct 2008 00:00:00 GMT</pubDate>
      </item>
   
      <item>
         <title><![CDATA[Automatic Properties and Object Initializers]]></title>
         <author><![CDATA[vinay]]></author>
         <description><![CDATA[<b>Automatic Properties<br></b><br>In this post we will look at some of the new features added in C# 3.0. The first of these will be automatic properties. Automatic properties gives us with a shorthand notation for defining a new property.<pre class="code"><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red0\green128\blue0;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 AutoProp\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 string\cf0  AddedBy \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??    \cf1 public\cf0  \cf4 DateTime\cf0  AddedDate \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf1 int\cf0  ViewCount \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\par ??    \cf5 //Normal way of defining property\par ??\cf0     \cf1 private\cf0  \cf1 int\cf0  id;\par ??    \cf1 public\cf0  \cf1 int\cf0  Id\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  id; \}\par ??        \cf1 set\cf0  \{ id = \cf1 value\cf0 ; \}\par ??    \}\par ??\}}
-->
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">AutoProp</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;{</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> AddedBy { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: rgb(43, 145, 175);">DateTime</span> AddedDate { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> ViewCount { <span style="color: blue;">get</span>; <span style="color: blue;">private</span> <span style="color: blue;">set</span>; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//Normal way of defining property</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">int</span> id;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> Id</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> id; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { id = <span style="color: blue;">value</span>; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;14</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;15</span>&nbsp;}</p></div></pre> Notice that the first 2 properties do not have Getters or Setters, what is happing is that the C# compiler generates the getters and setters automatically. Also notice that the fields are declared as public unlike the private field id. The ViewCount field in the snippet is interesting, because it has a private set. What this means is that you cant directly assign a value to this field from an object of the class. Thing to note in automatic properties are that you cant add any logic for an automatic property and also you cant create read-only automatic properties.<br><br><b>Object Initializers</b><br><br>Object initializers allows us to initialize the properties of an object while its being created. <br><pre class="code"><!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 Product\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 int\cf0  Id \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??    \cf1 public\cf0  \cf1 string\cf0  Name \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 class\cf0  \cf4 ObjectInit\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 void\cf0  SetProduct()\par ??    \{\par ??        \cf5 //Normal way\par ??\cf0         \cf4 Product\cf0  prod = \cf1 new\cf0  \cf4 Product\cf0 ();\par ??        prod.Id = 1;\par ??        prod.Name = \cf6 "Product1"\cf0 ;\par ??\par ??        \cf5 //New way\par ??\cf0         \cf4 Product\cf0  newProd = \cf1 new\cf0  \cf4 Product\cf0 () \{ Id = 2, Name = \cf6 "Product2"\cf0  \};\par ??    \}\par ??\}}
-->
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">Product</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;{</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> Id { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;}</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;<span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: rgb(43, 145, 175);">ObjectInit</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;{</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> SetProduct()</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">//Normal way</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Product</span> prod = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Product</span>();</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; prod.Id = 1;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;14</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; prod.Name = <span style="color: rgb(163, 21, 21);">"Product1"</span>;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;15</span>&nbsp;</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;16</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">//New way</span></p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;17</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175);">Product</span> newProd = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Product</span>() { Id = 2, Name = <span style="color: rgb(163, 21, 21);">"Product2"</span> };</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;18</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;"><span style="color: rgb(43, 145, 175);">&nbsp;&nbsp;&nbsp;19</span>&nbsp;}</p></div></pre>For eg. in the above snippet, the Product class has 2 properties Id and Name. In the ObjectInit class, i am creating 2 objects of the Product class viz. prod and newProd. The new Prod is initialized directly when the object is created unlike the prod where first the object is created and then the properties are initialized.<br><br>I will continue on other new features in my next post<br><br><br>]]></description>
         <link><![CDATA[http://www.simplyvinay.com/Post/6/Automatic-Properties-and-Object-Initializers.aspx]]></link>
         <pubDate>Tue, 01 Jul 2008 00:00:00 GMT</pubDate>
      </item>
   
         </channel>
      </rss>  
   
