Search code examples
c#compilationsyntactic-sugarilspy

How to get ILSpy to show compiler generated code


I've written and built this app:

namespace Test
{
    
    class Program
    {
        static void Main(string[] args)
        {
            var myClass = new MyClass();
            foreach (var item in myClass.CountFrom(1, 4))
            {
                Console.WriteLine(item);                
            }
            Console.Read();
        }
    }

    public class MyClass
    {
        public IEnumerable<int> CountFrom(int start, int limit)
        {
            for (int i = start; i <= limit; i++)
            {
                yield return i;
            }
        }
    }
}

When I view the code in ILSpy, based on the .exe in the bin/Debug folder, it doesn't show the state machine code I was hoping to see: enter image description here

How can I get it to show the compiler generated code?


Solution

  • You can go to "View->Option" and under the "Decompiler" tab, you can uncheck "C#2.0->Decompile enumerator(yield return)" : enter image description here