Lesson Weekend

In the last lesson, we built and ran our first project. Let's take a closer look at the process of compiling and running code in C#.

Compilation


Compilation is the process of assembling information from various sources into one location. In software development, this means transforming the code a developer writes into machine readable code which can be executed by a micro-processor.

All code is compiled. But this can happen in more than one way.

JIT

When we write a line of JavaScript code and pass it into the browser, it automatically runs. There is a direct relationship between the code we write and the behaviors we observe and interact with. In fact, the file the browser references is the same file we've written.

However, a compile action does happen with JavaScript in the browser. The code is evaluated and the actions within the code are carried out, but this happens live, as the user loads or interacts with the code. This type of compilation is called Just In Time compiling (JIT for short) because the code is compiled on-demand at the server or client level.

AOT

With C#, our computers do not interpret the code we write. Instead, our code is a set of instructions to build a program.

With this type of compilation, our code must be compiled into a different format that is optimized for our machines. The code is often compiled to binary files and is saved to disc separate from the code we write. It would be very challenging for humans to read this code.

This compilation happens long before a user ever encounters our program and is called Ahead Of Time compiling or AOT for short.

.NET 5

Several steps occur when we run the command dotnet build.

First, the runtime, or environment that processes the code, is invoked. In the case of .NET 5, this is a process called MSBuild. The MSBuild process looks through our project file and decides what resources it will need to build our program (just as a cook might decide which ingredients are needed for a recipe).

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
    </PropertyGroup>

</Project>

In the big picture, here is what happens in this code:

  • We tell the compiler we are using "Microsoft.NET.Sdk", a software development kit which contains the C# language and many libraries of information.
  • We tell the runtime that we would like an executable, or Exe output, meaning a program that can be run.
  • We also specify that we will be using the net5.0 framework, which contains a lot of our code libraries. We'll learn more about those later.

Overview


  • In modern programming languages, source code must be translated into machine code before computers can understand it. Some languages and platforms do this automatically, but others like C# must be explicitly compiled.

Terminology


  • Compiling: Translating one type of code into another type. Generally this refers to translating a more human-readable type of code into a more complex type of code meant only for computers.

  • Just in Time: When code is compiled as a user interacts with it, as is the case with JavaScript.

  • Ahead of Time: When code is compiled ahead of time, as is the case with C#.

Lesson 2 of 5
Last updated more than 3 months ago.