Dot Net For All

Creating my first .NET Core CLI project

Hello, In this article I will show you how we can create a .NET core cli project without the help of visual studio. You can check this article about downloading and installing the .NET core on your machine. In the same article I showed how we can create web application in the .NET core platform.

Once you have downloaded the .NET core on your system. You can make use of CLI to get started.

You can type “dotnet –help” command to see all the available command options as shown in the figure below.

 

 

.NET Core CLI

Under commands you can see various command parameters Out of these new, restore, build and run are very important.

new project can be created using “new” command.

“restore” is used to restore the nuget package dependencies of the project. The dependencies of the .NET core project are present in the nuget. If does not depend on the dependencies present on the system. It always try to restore the dependencies from nuget.

“build” is used to compile and build the project.

“run” is used to see the output of the project after compiling it.

Everything in the .NET core is dependency which in turn helps to keep the project clean and lean.

Ok now again go to the command prompt and type “dotnet new” and you can see the different type of projects  which are supported by .NET core as shown in the figure below.

We can see in the above figure different type of project templates supported by .net core. These are Console application, Class library, ASP.NET core empty etc.

Lets start by creating a console application. I have already created a folder in my system where I will be creating the project. The folder is “C:\Personal\SampleProjects\DotnetCoreCconsole”. Let go to this folder in command prompt and type “dotnet new console” as shown below.

In the above figure first command is for creating the project and second command is for listing the files in the folder. As we can see in the second command two files are created in the folder i.e. DotnetCoreCconsole.csproj and Program.cs.

Now lets see the contents of these two files. Let first check the DotnetCoreCconsole.csproj file

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

</Project>

Unlike the previous versions of the .NET framework this .csproj file is very light weight and it contains very few elements at the beginning.

The property group element contains the projects properties like what kind of project is it and the target framework. This file will also contain all the nuget files that our project requires.

The other file is Program.cs. Lets see its contents

using System;

namespace DotnetCoreCconsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

This file contains the entry point for the project named as Main() function. And it only depends only on one dependency named as system.

Now lets run the “dotnet restore”, “dotnet build” and “dotnet run” commands. 

Executing the “restore” command will help to restore the packages and it creates a folder name obj in the project folder.

Executing the “build” command builds the project and creates a .dll file in the “bin/Debug” folder. And last but not the least “dotnet run” helps to execute the program which in our case is a console output as shown below.

Conclusion:

In this article I have demonstrated how we can create a sample .NET core application using CLI commands. You have also seen some of the .NET core CLI commands in actions and their uses.

 

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview