Wrox Programmer Forums
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 2nd, 2010, 01:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default Learning IL

Its time to bite the bullet. As MS throws more and more functional programming techniques at us, and we all start tossing lambdas around, and passing functions as arguments to functions, and the compiler starts auto-generating more and more of our code for us, and Reflector starts getting its C# interpretation of the auto-generated code "wronger and wronger", I find my self writing more and more code I can't understand - without understanding IL.

Can anyone recommend some essential IL for dummies resources? Books, walk-throughs, references, videos, anything...

Here's a couple examples of what I mean.

Why does:

Code:
var actionList = newList<Func<int>>();
 
foreach (var value inEnumerable.Range(0, 10))
{
   actionList.Add(() => value);
}
 
actionList.ForEach(func => Console.Write("{0} ", func()));
print '9 9 9 9 9 9 9 9 9 9' while

Code:
var actionList = newList<Func<int>>();
 
foreach (var value inEnumerable.Range(0, 10))
{
   int myValue = value;
   actionList.Add(() => myValue);
}
 
actionList.ForEach(func => Console.Write("{0} ", func()));
prints '0 1 2 3 4 5 6 7 8 9'.

Reflector's C# interpretation is worthless. But the IL shows that the auto-generated class that captures the local variable gets its constructor called once in the first example (the local variable instance is reused), and multiple times in the second example (a new instance of the captured variable is created).

Or iterator blocks (state machines):


Code:
staticIEnumerator<int> GetCounter()
{
 for (int count = 0; count < 10; count++)
 {
     yield return count;
 }
}
gets "compiler-generated" as:

Code:
privatebool MoveNext()
{
switch (_state)
{
   case 0:
       _state = -1;
       _count_1 = 0;
       while (_count < 10)
       {
           _current = _count;
           _state = 1;
           return true;
   Label_0046:
           _state = -1;
           _count++;
       }
   break;
 
   case 1:
       goto Label_0046;
}
return false;
}
which is illegal C# 'cause the label is out of the 'goto' statements scope, so to understand whats happening we need:

Code:
 
publicbool MoveNext()
{
switch (_state)
{
   case 0:
       _state = -1;
       _count = 0;
       goto endLoop;
   loop:
       _current = _count;
       _state = 1;
       return true;
   Label_0046:
       _state = -1;
       _count++;
   endLoop:
       if (this._count < 10)
       goto loop;
       break;
 
   case 1:
       goto Label_0046;
   }
return false;
}
'cus IL doesn't know about 'while' loops.

That sort of thing. Stuff thats only understandable if you understand IL, which I'm seeing more and more of.

Bob
 
Old January 2nd, 2010, 01:25 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Regarding your first point you could do worse than check out this blog post:

http://blogs.msdn.com/ericlippert/ar...d-harmful.aspx
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
Bob Bedell (January 2nd, 2010)
 
Old January 2nd, 2010, 01:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Thanks Sam,

I'll spend some time with it today.

Regarding my second point, I've found these two helpful:

http://social.msdn.microsoft.com/For...4-5af157908ed4
http://social.msdn.microsoft.com/For...d-2cbcb72f6f18

Maybe IL for dummies is an oxymoron. I opened a copy of Serge Lidin's "Inside Microsoft .NET IL Assembler" once, and my eyes glazed over.

Be nice if there was like a "What every C# user needs to know about IL" title around somewhere.
 
Old January 2nd, 2010, 01:50 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

There is a book called "CLR via C#" which I think does what you might be after.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 2nd, 2010, 02:20 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Pre-ordered it at Amazon about three weeks ago. The 3rd edition is released in February. I'm a big Richter fan, and I'll bet you're right. He probably does cover a lot of the of the functional programming stuff. And he typically does it better than anyone.

Thanks.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Without modifying source code if we compile again, will it generate IL again? JitenSharma C# 2 March 22nd, 2009 02:41 AM
IL Disassembler elvisfeverr General .NET 1 July 14th, 2008 02:31 AM
E-learning dineshv Flash (all versions) 1 January 4th, 2007 01:35 AM
Learning C# Jael C# 1 October 19th, 2005 07:54 PM
Difference between MSIL and IL sumsin General .NET 2 March 14th, 2005 08:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.