Share via


.NET Reflector on IL generated by C# 3.0 (LINQ)

Nothing like using .NET Reflector. The following C# code:

public class Student {

   public string First { get; set; }

   public string Last { get; set; }

   public int ID {get; set;}

   public int[] Scores;

}

class Program {

   static void Main(string[] args) {

   List<Student> students = new List<Student> { new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new int[] {97, 92, 81, 60}},

                                                   new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new int[] {75, 84, 91, 39}},

                                                   new Student {First="Sven", Last="Mortensen", ID=113, Scores= new int[] {88, 94, 65, 91}},

               new Student {First="Cesar", Last="Garcia", ID=114, Scores= new int[] {97, 89, 85, 82}},

                                                   new Student {First="Debra", Last="Garcia", ID=115, Scores= new int[] {35, 72, 91, 70}},

                                                   new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new int[] {99, 86, 90, 94}},

                                                   new Student {First="Hanying", Last="Feng", ID=117, Scores= new int[] {93, 92, 80, 87}},

                                                   new Student {First="Hugo", Last="Garcia", ID=118, Scores= new int[] {92, 90, 83, 78}},

                                                   new Student {First="Lance", Last="Tucker", ID=119, Scores= new int[] {68, 79, 88, 92}},

                                                   new Student {First="Terry", Last="Adams", ID=120, Scores= new int[] {99, 82, 81, 79}},

                    new Student {First="Eugene", Last="Zabokritski", ID=121, Scores= new int[] {96, 85, 91, 60}},

                                                   new Student {First="Michael", Last="Tucker", ID=122, Scores= new int[] {94, 92, 91, 91} }

                                                 };

      IEnumerable<Student> studentQuery = from student in students

      where student.Scores[0] > 90 && student.Scores[3] < 80

    orderby student.Scores[0] descending

      select student;

      foreach (Student s in studentQuery) {

      Console.WriteLine("{0}, {1} {2}", s.Last, s.First, s.Scores[0]);

      }

      IEnumerable<IGrouping<char, Student>> studentQuery2 = from student in students

      group student by student.Last[0];

      foreach (IGrouping<char, Student> group in studentQuery2) {

      Console.WriteLine(group.Key);

         foreach (Student s in group) {

         Console.WriteLine(" {0}, {1}",

                              s.Last, s.First);

      }

   }

}

 

is actually compiled to something similar to:

private static void Main(string[] args) {

   Student s;

   IEnumerator<Student> CS$5$0000;

   List<Student> <>g__initLocal0 = new List<Student>();

   Student <>g__initLocal1 = new Student();

  <>g__initLocal1.First = "Svetlana";

   <>g__initLocal1.Last = "Omelchenko";

   <>g__initLocal1.ID = 0x6f;

   <>g__initLocal1.Scores = new int[] { 0x61, 0x5c, 0x51, 60 };

   <>g__initLocal0.Add(<>g__initLocal1);

   Student <>g__initLocal2 = new Student();

   <>g__initLocal2.First = "Claire";

   <>g__initLocal2.Last = "O\u2019Donnell";

   <>g__initLocal2.ID = 0x70;

   <>g__initLocal2.Scores = new int[] { 0x4b, 0x54, 0x5b, 0x27 };

   <>g__initLocal0.Add(<>g__initLocal2);

   Student <>g__initLocal3 = new Student();

   <>g__initLocal3.First = "Sven";

   <>g__initLocal3.Last = "Mortensen";

   <>g__initLocal3.ID = 0x71;

   <>g__initLocal3.Scores = new int[] { 0x58, 0x5e, 0x41, 0x5b };

   <>g__initLocal0.Add(<>g__initLocal3);

   Student <>g__initLocal4 = new Student();

   <>g__initLocal4.First = "Cesar";

   <>g__initLocal4.Last = "Garcia";

   <>g__initLocal4.ID = 0x72;

   <>g__initLocal4.Scores = new int[] { 0x61, 0x59, 0x55, 0x52 };

   <>g__initLocal0.Add(<>g__initLocal4);

   Student <>g__initLocal5 = new Student();

   <>g__initLocal5.First = "Debra";

   <>g__initLocal5.Last = "Garcia";

   <>g__initLocal5.ID = 0x73;

   <>g__initLocal5.Scores = new int[] { 0x23, 0x48, 0x5b, 70 };

   <>g__initLocal0.Add(<>g__initLocal5);

   Student <>g__initLocal6 = new Student();

   <>g__initLocal6.First = "Fadi";

   <>g__initLocal6.Last = "Fakhouri";

   <>g__initLocal6.ID = 0x74;

   <>g__initLocal6.Scores = new int[] { 0x63, 0x56, 90, 0x5e };

   <>g__initLocal0.Add(<>g__initLocal6);

   Student <>g__initLocal7 = new Student();

   <>g__initLocal7.First = "Hanying";

   <>g__initLocal7.Last = "Feng";

   <>g__initLocal7.ID = 0x75;

   <>g__initLocal7.Scores = new int[] { 0x5d, 0x5c, 80, 0x57 };

   <>g__initLocal0.Add(<>g__initLocal7);

   Student <>g__initLocal8 = new Student();

   <>g__initLocal8.First = "Hugo";

   <>g__initLocal8.Last = "Garcia";

   <>g__initLocal8.ID = 0x76;

   <>g__initLocal8.Scores = new int[] { 0x5c, 90, 0x53, 0x4e };

   <>g__initLocal0.Add(<>g__initLocal8);

   Student <>g__initLocal9 = new Student();

   <>g__initLocal9.First = "Lance";

   <>g__initLocal9.Last = "Tucker";

   <>g__initLocal9.ID = 0x77;

   <>g__initLocal9.Scores = new int[] { 0x44, 0x4f, 0x58, 0x5c };

   <>g__initLocal0.Add(<>g__initLocal9);

   Student <>g__initLocala = new Student();

   <>g__initLocala.First = "Terry";

   <>g__initLocala.Last = "Adams";

   <>g__initLocala.ID = 120;

   <>g__initLocala.Scores = new int[] { 0x63, 0x52, 0x51, 0x4f };

   <>g__initLocal0.Add(<>g__initLocala);

   Student <>g__initLocalb = new Student();

   <>g__initLocalb.First = "Eugene";

   <>g__initLocalb.Last = "Zabokritski";

   <>g__initLocalb.ID = 0x79;

   <>g__initLocalb.Scores = new int[] { 0x60, 0x55, 0x5b, 60 };

   <>g__initLocal0.Add(<>g__initLocalb);

   Student <>g__initLocalc = new Student();

   <>g__initLocalc.First = "Michael";

   <>g__initLocalc.Last = "Tucker";

   <>g__initLocalc.ID = 0x7a;

   <>g__initLocalc.Scores = new int[] { 0x5e, 0x5c, 0x5b, 0x5b };

   <>g__initLocal0.Add(<>g__initLocalc);

   List<Student> students = <>g__initLocal0;

   if (Program.<>9__CachedAnonymousMethodDelegate10 == null) {

   Program.<>9__CachedAnonymousMethodDelegate10 = new Func<Student, bool>(Program.<Main>b__d);

   }

   if (Program.<>9__CachedAnonymousMethodDelegate11 == null) {

   Program.<>9__CachedAnonymousMethodDelegate11 = new Func<Student, int>(Program.<Main>b__e);

   }

   IEnumerable<Student> studentQuery = Enumerable.OrderByDescending<Student, int>(Enumerable.Where<Student>(students,

                                             Program.<>9__CachedAnonymousMethodDelegate10),

                                                                                    Program.<>9__CachedAnonymousMethodDelegate11);

   using (CS$5$0000 = studentQuery.GetEnumerator()) {

   while (CS$5$0000.MoveNext()) {

      s = CS$5$0000.Current;

         Console.WriteLine("{0}, {1} {2}", s.Last, s.First, s.Scores[0]);

      }

   }

   if (Program.<>9__CachedAnonymousMethodDelegate12 == null) {

   Program.<>9__CachedAnonymousMethodDelegate12 = new Func<Student, char>(Program.<Main>b__f);

   }

   IEnumerable<IGrouping<char, Student>> studentQuery2 = Enumerable.GroupBy<Student, char>(students,

                                                                                       Program.<>9__CachedAnonymousMethodDelegate12);

   foreach (IGrouping<char, Student> group in studentQuery2) {

   Console.WriteLine(group.Key);

      using (CS$5$0000 = group.GetEnumerator()) {

      while (CS$5$0000.MoveNext()) {

         s = CS$5$0000.Current;

            Console.WriteLine(" {0}, {1}", s.Last, s.First);

         }

      }

   }

}

Comments

  • Anonymous
    February 18, 2007
    "Studio" forse non è il termine corretto, dal momento che presuppone un minimo di metodo e, magari, di