KB Controls is reader-supported. When you buy through links on our site, we may earn an affiliate commission. Read more in our disclaimer
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.
In this post, we learn how to debug a C# .NET Console application using the Console messages and Visual Studio Code's debugger. We also learn how to use conditional statements to avoid exceptional situations.
In this part of the series, we learn how to add interactivity to our application. Along the way, we will learn about methods, arrays, and string manipulation in C#.