In this post, we will create our very first console application in .NET Core, we will see how we can build .NET Core based applications without using any IDE or Visual Studio. We will be using Command Line to create, build and run the application. If we don’t need all the fancy features that Visual Studio and Visual Studio Code offers, then we can build a .NET Core application with just a note pad, the only thing we would need is the .NET Core SDK installed on the machine, and all other actions that we do with Visual Studio can be invoked using the CLI. We will see how it can be done.
Checking .NET Core Installation
Before creating the .NET Core project, it is an obvious thing that we should have .NET Core installed on our machine which can be checked by opening a Command Prompt Window or PowerShell window and typing in it the
dotnet command. If the .NET Core is not already installed on the machine, we will get an error as it will not be able to recognize the command:
Installing .NET Core SDK
As .NET Core is all about cross platform, the SDK is available for different platforms which includes Windows 32-bit and 64-bit, MacOS and Linux. If we go to the official download link, we can see multiple options available from which we can choose as per our need or convenience.
The following is the list taken from the official .NET Core download link:
We will be creating the application using Windows, so let’s download the SDK installer executable for Windows. I downloaded the Installer of Windows (x64) as I have 64-bit Windows installed on my machine.
After downloading the installer, proceed to install the SDK:
This installer will install all the necessary components needed to develop .NET Core application and running it which includes .NET Core RunTime, SDK and other things as well. The installation will take 2 - 3 minutes to do the installation, and if all the things go right in installation, you will see the following window acknowledging the successful installation:
As we were able to install the SDK successfully, now open the command prompt again and type
dotnet command again. I am using powershell for this and executing the command gives you back some output that means .NET Core is being setup correctly:
.NET Core CLI provides different commands to create new project, build, clean and all other commands that we normally invoke using Visual Studio. There is a complete list of commands documented on the official documentation page and all the commands can be seen at this link.
Creating Console Application
Now let’s create the simplest famous
Hello World console application using command prompt in .NET Core. If all the previous steps have been completed correctly, open the Command Prompt and create a new directory which will contain the source code for the application.
Write the following command on cmd to create the directory:
mkdir First DotNetCoreApp
Now open the directory using the following command:
cd First DotNetCoreApp
From the above image, we can verify that we have opened the correct directory.
Adding Project Template
.NET Core comes with its own CLI tools that enable to create a new project using commands in Command Prompt or Powershell without even opening up the IDE either Visual Studio or VS Code.
We will type
dotnet new on command line and press enter which will list down all the templates that can be created using this command.
After running the command, we will see few things listed which includes the different flags which are available to do different things which are also available within Visual Studio but the support is added via Command Line too which is great.
If we go slightly down, we can see all the templates listed which are available via CLI tools:
Now let’s run the command for creating a new Console Application, so write
dotnet new and press enter key and this will create a new Console Application project in our working directory:
Our project has been created successfully, but to double check and make sure the command worked fine, we can list the directory content and we should be able to see csproj, Program.cs and other files for the application.
Now let’s run the application by executing the
dotnet run command in CMD:
One thing to remember here is that if you are creating the project using .NET Core 1.0 SDK, then before the
dotnet run command, you would need to execute the
dotnet restore command which will restore all the NuGet package dependencies of the project, but for .NET Core 2.0, we don’t need to execute this command as calling the
dotnet run makes sure to restore the NuGet package dependencies before running the code.
restore command was called when we executed the command
dotnet new and NuGet packages were restored at that time, but it is also called on
run as well, which can be verified from this GitHub announcement.
We saw that how we can create, build and run .NET Core based application using either Command Prompt or PowerShell, the only thing that we need to start developing the application without IDE is to install the .NET Core SDK on the machine and we are done.