title: How to disassemble .NET code right?
body: <p>I'm trying to disassemble my C# code and then debug it on assembly language level.
  Let's say we have a simple C# method:</p>
  
  <pre><code>var a = 1235;
  var b = ++a;
  var c = ++b;
  
  Console.WriteLine("test");
  Console.ReadKey();
  </code></pre>
  
  <p>I've found two different ways how to get an assembly code.
  The first one is to start C# code debugging in VS and then open Disassembly window. Here we have the following code.</p>
  
  <p><a href="https://i.stack.imgur.com/cWWD1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cWWD1.png" alt="first method assembly"></a></p>
  
  <p>Everything is OK and assembly code is pretty much simple and short but the problem is that the logic of this assembly code differs from the logic of the IL code generated by <em>ildasm</em>.</p>
  
  <p><a href="https://i.stack.imgur.com/88mJP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/88mJP.png" alt="cil code"></a></p>
  
  <p>So here is the second way. We can compile C# code, use <em>ildasm</em> to get the IL code from PE file and then use <em>ilasm</em> to generate PE file back. Now we have the following assembly code.</p>
  
  <p><a href="https://i.stack.imgur.com/38NbG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/38NbG.png" alt="second method code"></a></p>
  
  <p>As you can see this assembly code is more like the IL code but it contains much more instructions and it is more complicated.</p>
  
  <p>AFAIK C# compiles to CIL code and then to an assembly code in both cases. But it seems to be that in the first way it just compiles to an assembly code.</p>
  
  <p>So the question is why the assembly code of the first method differs from the IL code? And why the assembly code of the first method differs from the assembly code of the second method?</p>