Welcome to another part of the series C# From Scratch, a course dedicated to teaching you everything you need to know to be productive with the C# programming language.
In the previous part of the series, we learned how to create a new .NET project using the .NET CLI and the dotnet new command. We used this command to create a .NET Console Application. If you missed it, that part of the series is available here.
In this part of the series, we’ll explore the structure of the .NET project that we have just created and learn how to run the project in a Command Prompt window.
Exploring the Project
Use the ‘dir’ command to list the contents of the src directory, where we have created the GradeBook project.
You can see that the directory now contains two files — Program.cs and src.csproj
The .csproj extension indicates that this is a C# Project file. It contains information about the project.
The .cs extension indicates that this is a C# source code file. This file contains all of the source code for our application.
Running the Application
While inside the src directory, you can use the command ‘dotnet run’ to run your application.
When you use this command, the application runs and the output ‘Hello World!’ is printed to the Command Prompt window.
We know that the application is working since it produces the default output, but what exactly is happening when we use the dotnet run command?
Behind the Scenes
When you use the dotnet run command, .NET automatically does several things to make your project ready to run. In this section, we’ll look at the steps .NET takes behind the scenes to run your application.
The first thing that happens is .NET implicitly runs the command ‘dotnet restore’. The dotnet restore command tells .NET to go out and get the correct version of any packages used by your application.
Packages are pieces of codes that other developers have written and you can use in your application. In the world of .NET, the package system is called NUGET.
All of the packages being used by your application are recorded in the .csproj file, along with the version of the package being used. When the dotnet restore command is executed, .NET goes out and gets each package listed in your .csproj file from the internet.
The packages that are used in your application are called external dependencies.
In our basic project, we don’t have any external dependencies so nothing happens when .NET runs this command.
Next, .NET runs the ‘dotnet build’ command implicitly.
This command tells .NET to compile your source code files into a binary file that can execute on your machine.
The tool that does this is the C# compiler. The C# compiler takes all of the source code files in your project as an input and outputs a single binary representation of your source code. This binary format is faster to execute on your machine.
The output file is a dll file.
Dll stands for Dynamic Linked Library. In .NET, this is referred to as an assembly.
Finally, the dotnet run command is executed.
This command tells .NET to bring the application to life using the .NET Core Runtime and to execute the instructions in the dll file.
It’s important to note that if you want to run your application, you need to use the .NET Core Runtime. It’s not possible to run the application via the dll file directly.
This is because it’s the .NET Core Runtime that knows how to launch your application, manage memory, convert the instructions in the dll file into instructions that your processor understands and tear down the application when it’s finished running.
In this part of the series, we learned how to run a .NET application using the dotnet run command. We also learned what .NET is doing in the background to convert our project into an application that can be run on our machine.
In the next part of the series, we’ll learn how to open the project in Visual Studio Code and look at the most important parts of our code editor. You can find that part of the series here.
Missed a part of the series? You can find an overview of the series and links to all previous parts on the index page.