From the course: C# Essential Training 1: Types and Control Flow

C# compiles to MSIL

- [Instructor] When learning a new programming language, I know I understand it better when I also learn the context in which it executes. For C#, that means understanding that code written in C# compiles to MSIL, or Microsoft Intermediate Language. C# is one of several .NET languages that all compile to MSIL. F# is a functional programming language, and Visual Basic .NET are two other languages, supported by Microsoft, with their own compilers that each know how to take those source files in their particular programming language and compile it to Microsoft Intermediate Language. One of the benefits of this is that the C# language can evolve independent of the runtime. So back with the .NET Runtime 4, .NET Framework 4 from years ago, we had a variety of different versions of C# that all were able to compile down to MSIL that could run on that one common runtime, and that gives the language flexibility to grow and change independent of the runtime. These days, for example with .NET 6, released in the fall of 2021, you'll see that C# tends to release with .NET with new versions of the compiler. As an example, we look at this simple hello world application written in C# and compile it into an executable. So on the bottom pane or window, I've got a person class, and in the top, a simple hello world, where I create that person and write a message out. We can open that in a tool called ILDASM or IL Disassembler, that allows us to see from a compiled .NET executable or library, what the intermediate language is. So I can browse into LinkedIn Essentials here, to my person class, and we can see all those properties, things like first name, age, last name, have get methods and set methods. So if I go to the get last name, for example, I can now see what the MSIL looks like. And in this case, we can see that line that starts with IL and four zeros, it's loading an argument. And the next it's going to load a field. And for that, we've got the person, last name, k, underscore, underscore, backing field. That's the auto implemented field for our property definition, because our property simply identified the get and set, didn't put any particular code or expressions there so that was created for us automatically. Likewise, if we go to the set last name. We can see that we have a method on the first line there called set last name, takes in a string of value. And then as you go down, you can see on aisle 0002 there, that it sets a field. So now it's setting that backing field with the value that was passed in. Likewise, we can go down to the program, find our static void main. Now we can see all of those string literals like Matt and Milner. We're going to load string command, on the actual message, the hello from, the format there. And then that last one, we've got a call to our system console right line with the string. So all of that C# that we wrote all compiled down to this MSIL that can now get executed.

Contents