In this part of the series, we learn how to run a C# .NET Console application from the Command Prompt and what happens behind the scenes when you execute the dotnet run command.
Welcome to another edition of C# From Scratch, a course dedicated to teaching you everything you need to know to be productive with C#. If you’re new here, head over to the index to see all parts of the series.
In the last part of the series, we learned how to create a new C# .NET Console application.
In this part of the series, we’ll learn how to run a C# .NET Console application and look at what happens behind the scenes when we run our application.
You can use the dotnet run command to run a C# .NET Console application.
This command looks inside of the current folder for a .NET project. In this case, the dotnet run command will find Hello.csproj.
Once .NET has found a project, it automatically takes the necessary steps to run the project.
The application executes and prints “Hello World!” to the console window.
Now that we know how to run our C# .NET application, let’s talk about what is happening behind the scenes when you execute the dotnet run command.
.NET takes several steps to convert our C# .NET project into an application that can be run on your machine. In this section, we’ll talk about each step that .NET takes implicitly when you use the dotnet run command.
When you execute the dotnet run command, .NET implicitly executes another command called dotnet restore. So, what does dotnet restore do?
In .NET, there is a package system known as NuGet. A package system allows developers to leverage pieces of code written by other developers in their applications. These bits of code live in files called NuGet packages. Those NuGet packages are published to a package feed and are made available for developers to use.
In order to use a NuGet package in your application, you tell .NET that you are using a NuGet package by making a reference to the package in the application’s .csproj file.
When the dotnet restore command is run, .NET checks the .csproj file for references to NuGet packages and goes out to download the required package files.
Right now, our application doesn’t use any NuGet packages, but we will learn how to work with NuGet packages later when we talk about unit testing.
The dotnet build command converts your project’s source code into an executable file. This command compiles all of the source code files in your project into a binary file using a tool called a compiler.
You can see that the output of the dotnet build command is a file called Hello.dll. Windows users may recognize the .dll extension as a Dynamically Linked Library file but in .NET, this file is what we call an assembly file.
You can try to execute this file directly from the Command Prompt.
You can see that trying to run Hello.dll directly from the Command Prompt results in an error.
This error is saying that your machine could not find the runtime that is required to execute this file. The runtime that the machine is looking for is the .NET Runtime.
So, to run a .NET assembly, we need to use the .NET Runtime. We can tell .NET to execute an application using the dotnet run command as before.
In this part of the series, we learned how to run a C# .NET Console application from the Command Prompt. We also looked in detail at what happens behind the scenes when you execute the dotnet run command.
Now that we are familiar with creating and running .NET applications, we can move on to learning how to add functionality to our applications. In the next part of the series, we will set up our code editor and add interactivity to our Hello application.
Sign up to the mailing list to get a new post about industrial automation and controls engineering delivered to your inbox every week.
Learn the skills you need to start your journey as a PLC programmer. Enroll in PLC Bootcamp to learn how to write and test your first PLC program for free.