<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Interesting Linq Problem</title>
	<atom:link href="http://www.kinnarshah.in/index.php/2009/11/04/interesting-linq-problem/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kinnarshah.in/index.php/2009/11/04/interesting-linq-problem/</link>
	<description>Just Another Geek With Just Another Blog...</description>
	<lastBuildDate>Fri, 16 Jul 2010 16:50:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Sanil</title>
		<link>http://www.kinnarshah.in/index.php/2009/11/04/interesting-linq-problem/comment-page-1/#comment-14</link>
		<dc:creator>Sanil</dc:creator>
		<pubDate>Wed, 04 Nov 2009 20:27:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.kinnarshah.in/index.php/2009/11/04/interesting-linq-problem/#comment-14</guid>
		<description>Really interesting...
well! IMO that&#039;s correct to me...
the query will return a sorted enumerable collection

though, I will like it to write like this:

using System.Linq; 
namespace solution
{
 class Program 
 { 
  static void Main() 
  { 
    int[] data = { 1, 2, 3, 1, 2, 1 }; 
    foreach (var m in from m in data orderby m select m) 
      System.Console.Write(m); 
  } 
 }
}

Here the foreach block should work like this
foreach (var m in [from m in data orderby m select m] ) 

or

foreach(var m = [from m&#039; in data order by m&#039; select m&#039;])

that would eventually looks like on further abstraction
var m in m&#039; (where m&#039; is enumerable)

take things between [ ] as another block.
Here a block is code construct between braces 
{ /// A block }

when compiler will be working on above given code, it will take the two Ms (read as ams), and will internally create two different variables for this.
actually compiler does this often, where it needs to do things like
variable = variale * something; (here * is an operation)

Let me prove this by running a Reflection (System.Reflection) based Method Body (The main method) analysis:

Let&#039;s assume, if the main() body has construct like this:
public class program
{
    public void main()
    {
        int[ ] data = { 1, 2, 3, 1, 2, 1 }; 
        foreach (var num in data)
        {
            System.Console.Write(num);
        }
    }
}

 We will use above main() block, as given below to get our analysis done

    class Program 
    { 
      static void Main() 
      {  
         int[] data = { 1, 2, 3, 1, 2, 1 };
         foreach (var num in data)
         {
            System.Console.Write(num);
         } 
         
         new Analyse().Run();
      } 
    }

    class Analyse
    {
        public void Run()
        {
            Assembly asm = Assembly.GetAssembly(typeof(Program));
            MethodBody mb = asm.EntryPoint.GetMethodBody();
            System.Console.WriteLine(&quot;\nMethod Name: &quot;+asm.EntryPoint.Name);
            
            foreach (var locals in mb.LocalVariables)
            {
                System.Console.WriteLine(&quot;\n {0}&quot;, locals.LocalType.FullName);
            }
            System.Console.ReadKey();
        }
    }

The Run method would actually list all the variables that are present after actual compilation, this will include the variables we have declared explicitly or implicitly as well as helper variables introduced by compiler to store temporary results and perform calculations.

The output on listing local variables in compiled assembly is:
( The actual variables that are present after compilation)

Method Name: Main

System.Int32[]

System.Int32

System.Int32[]

System.Int32

System.Boolean

A Little explanation:

the first system.Int32[] is this array int[ ] data = { 1, 2, 3, 1, 2, 1 }; 

next System.Int32 will receive value at each iteration and will be used in WriteLine()

the second System.Int32[] refers to ForEach&#039;s copy of array. Remember foreach doesn&#039;t allow changing contents, so it makes a copy.

the second Int32 is an index to track current iterator location

last is a boolean which stores the result of condition check

p.s: this is all guess work :P

How did I decided that Ist System.int32 is what and what&#039;s the role of the second one. Let&#039;s edit our code a bit to play with a char array.

    class Program 
    { 
      static void Main() 
      {  
         char[] ch = { &#039;a&#039;, &#039;b&#039;, &#039;c&#039; };
         foreach (var num in ch)
         {
             System.Console.Write(num);
         }
         
         new Analyse().Run();
      } 
    }

    class Analyse
    {
        public void Run()
        {
            Assembly asm = Assembly.GetAssembly(typeof(Program));
            MethodBody mb = asm.EntryPoint.GetMethodBody();
            System.Console.WriteLine(&quot;\nMethod Name: &quot;+asm.EntryPoint.Name);
            
            foreach (var locals in mb.LocalVariables)
            {
                System.Console.WriteLine(&quot;\n {0}&quot;, locals.LocalType.FullName);
            }
            System.Console.ReadKey();
        }
    }

This time the output appears like this:
 
Method Name: Main

 System.Char[]

 System.Char

 System.Char[]

 System.Int32

 System.Boolean

so you can see that, system.char is getting the assigned value that will be WriteLined (outputted)
and thus comparing the ordinal similiarity between the two outputs it would be a right to think that first system.int32 is getting the current value of data (where iterator is pointing).

Back to our actual code,

    class Program 
    { 
      static void Main() 
      {  
         int[] data = { 1, 2, 3, 1, 2, 1 };

         char[] ch = { &#039;a&#039;, &#039;b&#039;, &#039;c&#039; };

         foreach (var m in from m in data orderby m select m)
             System.Console.Write(m);
         
         new Analyse().Run();
      } 
    }

    class Analyse
    {
        public void Run()
        {
            Assembly asm = Assembly.GetAssembly(typeof(Program));
            MethodBody mb = asm.EntryPoint.GetMethodBody();
            System.Console.WriteLine(&quot;\nMethod Name: &quot;+asm.EntryPoint.Name);
            
            foreach (var locals in mb.LocalVariables)
            {
                System.Console.WriteLine(&quot;\n {0}&quot;, locals.LocalType.FullName);
            }
            System.Console.ReadKey();
        }
    }

running same analysis on this code gives following output (explanation to each line is in corresponding brackets)

Method Name: Main

System.Int32[ ] 
(the actual data[] array)

System.Int32
(that will receive value at each iteration and will be WriteLined/outputted)

System.Collections.Generic.IEnumerator`1[[System.Int32, mscorlib blah blah blah... ]]
( the generic IEnumerator generated from LINQ expression obtained, now all classes implementing IEnumerator has a method called MoveNext( ), hence it will not require any additional Sytem.int32 indexer, hence here it is absent in output)

System.Boolean
(condition check result as previously stated)
From all this we get  that compiler handle duplicate references smartly, until we play by his rules (according to C# language specs)

making that Looong explanation short, here the var m (implicitly of type int32) is completely isolated from IEnumerator type &#039;m&#039; in LINQ query due to somehow invisible scope block that compiler can see and work with when they encounter complex expressions like LINQ queries in such constructs.

Hoping, that I&#039;m here right in what I&#039;m thinking, if you think I&#039;m somewhere wrong do comment your opinion and theory/proofs here.</description>
		<content:encoded><![CDATA[<p>Really interesting&#8230;<br />
well! IMO that&#8217;s correct to me&#8230;<br />
the query will return a sorted enumerable collection</p>
<p>though, I will like it to write like this:</p>
<p>using System.Linq;<br />
namespace solution<br />
{<br />
 class Program<br />
 {<br />
  static void Main()<br />
  {<br />
    int[] data = { 1, 2, 3, 1, 2, 1 };<br />
    foreach (var m in from m in data orderby m select m)<br />
      System.Console.Write(m);<br />
  }<br />
 }<br />
}</p>
<p>Here the foreach block should work like this<br />
foreach (var m in [from m in data orderby m select m] ) </p>
<p>or</p>
<p>foreach(var m = [from m' in data order by m' select m'])</p>
<p>that would eventually looks like on further abstraction<br />
var m in m&#8217; (where m&#8217; is enumerable)</p>
<p>take things between [ ] as another block.<br />
Here a block is code construct between braces<br />
{ /// A block }</p>
<p>when compiler will be working on above given code, it will take the two Ms (read as ams), and will internally create two different variables for this.<br />
actually compiler does this often, where it needs to do things like<br />
variable = variale * something; (here * is an operation)</p>
<p>Let me prove this by running a Reflection (System.Reflection) based Method Body (The main method) analysis:</p>
<p>Let&#8217;s assume, if the main() body has construct like this:<br />
public class program<br />
{<br />
    public void main()<br />
    {<br />
        int[ ] data = { 1, 2, 3, 1, 2, 1 };<br />
        foreach (var num in data)<br />
        {<br />
            System.Console.Write(num);<br />
        }<br />
    }<br />
}</p>
<p> We will use above main() block, as given below to get our analysis done</p>
<p>    class Program<br />
    {<br />
      static void Main()<br />
      {<br />
         int[] data = { 1, 2, 3, 1, 2, 1 };<br />
         foreach (var num in data)<br />
         {<br />
            System.Console.Write(num);<br />
         } </p>
<p>         new Analyse().Run();<br />
      }<br />
    }</p>
<p>    class Analyse<br />
    {<br />
        public void Run()<br />
        {<br />
            Assembly asm = Assembly.GetAssembly(typeof(Program));<br />
            MethodBody mb = asm.EntryPoint.GetMethodBody();<br />
            System.Console.WriteLine(&#8220;\nMethod Name: &#8220;+asm.EntryPoint.Name);</p>
<p>            foreach (var locals in mb.LocalVariables)<br />
            {<br />
                System.Console.WriteLine(&#8220;\n {0}&#8221;, locals.LocalType.FullName);<br />
            }<br />
            System.Console.ReadKey();<br />
        }<br />
    }</p>
<p>The Run method would actually list all the variables that are present after actual compilation, this will include the variables we have declared explicitly or implicitly as well as helper variables introduced by compiler to store temporary results and perform calculations.</p>
<p>The output on listing local variables in compiled assembly is:<br />
( The actual variables that are present after compilation)</p>
<p>Method Name: Main</p>
<p>System.Int32[]</p>
<p>System.Int32</p>
<p>System.Int32[]</p>
<p>System.Int32</p>
<p>System.Boolean</p>
<p>A Little explanation:</p>
<p>the first system.Int32[] is this array int[ ] data = { 1, 2, 3, 1, 2, 1 }; </p>
<p>next System.Int32 will receive value at each iteration and will be used in WriteLine()</p>
<p>the second System.Int32[] refers to ForEach&#8217;s copy of array. Remember foreach doesn&#8217;t allow changing contents, so it makes a copy.</p>
<p>the second Int32 is an index to track current iterator location</p>
<p>last is a boolean which stores the result of condition check</p>
<p>p.s: this is all guess work <img src='http://www.kinnarshah.in/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>How did I decided that Ist System.int32 is what and what&#8217;s the role of the second one. Let&#8217;s edit our code a bit to play with a char array.</p>
<p>    class Program<br />
    {<br />
      static void Main()<br />
      {<br />
         char[] ch = { &#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217; };<br />
         foreach (var num in ch)<br />
         {<br />
             System.Console.Write(num);<br />
         }</p>
<p>         new Analyse().Run();<br />
      }<br />
    }</p>
<p>    class Analyse<br />
    {<br />
        public void Run()<br />
        {<br />
            Assembly asm = Assembly.GetAssembly(typeof(Program));<br />
            MethodBody mb = asm.EntryPoint.GetMethodBody();<br />
            System.Console.WriteLine(&#8220;\nMethod Name: &#8220;+asm.EntryPoint.Name);</p>
<p>            foreach (var locals in mb.LocalVariables)<br />
            {<br />
                System.Console.WriteLine(&#8220;\n {0}&#8221;, locals.LocalType.FullName);<br />
            }<br />
            System.Console.ReadKey();<br />
        }<br />
    }</p>
<p>This time the output appears like this:</p>
<p>Method Name: Main</p>
<p> System.Char[]</p>
<p> System.Char</p>
<p> System.Char[]</p>
<p> System.Int32</p>
<p> System.Boolean</p>
<p>so you can see that, system.char is getting the assigned value that will be WriteLined (outputted)<br />
and thus comparing the ordinal similiarity between the two outputs it would be a right to think that first system.int32 is getting the current value of data (where iterator is pointing).</p>
<p>Back to our actual code,</p>
<p>    class Program<br />
    {<br />
      static void Main()<br />
      {<br />
         int[] data = { 1, 2, 3, 1, 2, 1 };</p>
<p>         char[] ch = { &#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217; };</p>
<p>         foreach (var m in from m in data orderby m select m)<br />
             System.Console.Write(m);</p>
<p>         new Analyse().Run();<br />
      }<br />
    }</p>
<p>    class Analyse<br />
    {<br />
        public void Run()<br />
        {<br />
            Assembly asm = Assembly.GetAssembly(typeof(Program));<br />
            MethodBody mb = asm.EntryPoint.GetMethodBody();<br />
            System.Console.WriteLine(&#8220;\nMethod Name: &#8220;+asm.EntryPoint.Name);</p>
<p>            foreach (var locals in mb.LocalVariables)<br />
            {<br />
                System.Console.WriteLine(&#8220;\n {0}&#8221;, locals.LocalType.FullName);<br />
            }<br />
            System.Console.ReadKey();<br />
        }<br />
    }</p>
<p>running same analysis on this code gives following output (explanation to each line is in corresponding brackets)</p>
<p>Method Name: Main</p>
<p>System.Int32[ ]<br />
(the actual data[] array)</p>
<p>System.Int32<br />
(that will receive value at each iteration and will be WriteLined/outputted)</p>
<p>System.Collections.Generic.IEnumerator`1[[System.Int32, mscorlib blah blah blah... ]]<br />
( the generic IEnumerator generated from LINQ expression obtained, now all classes implementing IEnumerator has a method called MoveNext( ), hence it will not require any additional Sytem.int32 indexer, hence here it is absent in output)</p>
<p>System.Boolean<br />
(condition check result as previously stated)<br />
From all this we get  that compiler handle duplicate references smartly, until we play by his rules (according to C# language specs)</p>
<p>making that Looong explanation short, here the var m (implicitly of type int32) is completely isolated from IEnumerator type &#8216;m&#8217; in LINQ query due to somehow invisible scope block that compiler can see and work with when they encounter complex expressions like LINQ queries in such constructs.</p>
<p>Hoping, that I&#8217;m here right in what I&#8217;m thinking, if you think I&#8217;m somewhere wrong do comment your opinion and theory/proofs here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: <img src='http://www.kinnarshah.in/wp-content/plugins/rpx/images/google.png'/> Kinnar Shah</title>
		<link>http://www.kinnarshah.in/index.php/2009/11/04/interesting-linq-problem/comment-page-1/#comment-15</link>
		<dc:creator><img src='http://www.kinnarshah.in/wp-content/plugins/rpx/images/google.png'/> Kinnar Shah</dc:creator>
		<pubDate>Wed, 04 Nov 2009 18:42:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.kinnarshah.in/index.php/2009/11/04/interesting-linq-problem/#comment-15</guid>
		<description>@Sanil: dude... are you human???!!
sheer awesomeness!!!
bow down to thee... </description>
		<content:encoded><![CDATA[<p>@Sanil: dude&#8230; are you human???!!<br />
sheer awesomeness!!!<br />
bow down to thee&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
