Skip to main content

.NET CLI

Introduction

The .NET Command Line Interface (CLI) is a cross-platform tool that allows developers to create, build, test, run, and publish .NET applications directly from the command line. It's an essential tool for .NET developers as it provides a fast and efficient way to work with .NET projects without requiring a full-fledged Integrated Development Environment (IDE).

Whether you're working with C#, F#, or Visual Basic, the .NET CLI provides a consistent experience across different operating systems including Windows, macOS, and Linux. This makes it an incredibly versatile tool for modern development workflows.

Getting Started with .NET CLI

Installation

Before using the .NET CLI, you need to install the .NET SDK (Software Development Kit). You can download it from the official .NET website.

To verify your installation, open a terminal or command prompt and run:

bash
dotnet --version

This command should display the installed .NET SDK version:

7.0.306

Basic Structure of .NET CLI Commands

All .NET CLI commands follow this pattern:

dotnet [command] [arguments] [options]

Where:

  • dotnet is the command-line driver
  • command is the .NET CLI command you want to execute
  • arguments are specific to the command
  • options are flags that modify the command's behavior

Essential .NET CLI Commands

Getting Help

One of the most useful commands to remember is the help command:

bash
dotnet --help

This displays a list of available commands and brief descriptions. You can also get help for specific commands:

bash
dotnet new --help

Creating New Projects

The new command creates new projects, configuration files, and solution files based on templates:

bash
dotnet new console -n MyFirstApp

This command creates a new console application named "MyFirstApp". The output will look something like:

The template "Console App" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on MyFirstApp\MyFirstApp.csproj...
Determining projects to restore...
Restored MyFirstApp\MyFirstApp.csproj (in 112 ms).
Restore succeeded.

Common project templates include:

  • console - Console application
  • classlib - Class library
  • web - ASP.NET Core empty web application
  • webapi - ASP.NET Core Web API
  • mvc - ASP.NET Core Model-View-Controller application
  • razor - Razor Page application

To see all available templates:

bash
dotnet new list

Building Projects

To compile your application:

bash
dotnet build

A successful build looks like:

Microsoft (R) Build Engine version 17.6.8+c70978d4d for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

Determining projects to restore...
All projects are up-to-date for restore.
MyFirstApp -> C:\Projects\MyFirstApp\bin\Debug\net7.0\MyFirstApp.dll

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:01.57

Running Projects

To run your application:

bash
dotnet run

If your project has multiple files, you can specify the project:

bash
dotnet run --project MyFirstApp.csproj

Publishing Projects

To create a production-ready version of your application:

bash
dotnet publish -c Release

The -c Release flag specifies a Release configuration, which optimizes the code for production use.

Managing Solutions and Projects

Creating Solutions

A solution is a container for organizing related projects:

bash
dotnet new sln -n MySolution

Adding Projects to Solutions

After creating a solution, you can add projects:

bash
dotnet sln MySolution.sln add MyFirstApp/MyFirstApp.csproj

Managing NuGet Packages

To add a package reference:

bash
dotnet add package Newtonsoft.Json

To restore packages for a project:

bash
dotnet restore

Practical Examples

Example 1: Creating and Running a Simple Console Application

Let's create a simple "Hello World" application:

bash
dotnet new console -n HelloWorld
cd HelloWorld

Examine the Program.cs file, which should contain:

csharp
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

Run the application:

bash
dotnet run

Output:

Hello, World!

Example 2: Working with a Multi-Project Solution

Let's create a solution with a class library and a console app that uses it:

bash
# Create a solution
dotnet new sln -n LibraryDemo

# Create a class library
dotnet new classlib -n MathLibrary
cd MathLibrary

# Edit the class library to add functionality

Now modify the Class1.cs file in the MathLibrary project:

csharp
namespace MathLibrary
{
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}

public int Subtract(int a, int b)
{
return a - b;
}
}
}

Continue with creating the console application:

bash
cd ..
dotnet new console -n CalcApp

# Add projects to the solution
dotnet sln LibraryDemo.sln add MathLibrary/MathLibrary.csproj CalcApp/CalcApp.csproj

# Add a reference from CalcApp to MathLibrary
dotnet add CalcApp/CalcApp.csproj reference MathLibrary/MathLibrary.csproj

Now modify the Program.cs file in the CalcApp project:

csharp
using MathLibrary;

// Create a calculator instance
Calculator calc = new Calculator();

// Use the calculator
int sum = calc.Add(5, 3);
int difference = calc.Subtract(10, 4);

// Display results
Console.WriteLine($"5 + 3 = {sum}");
Console.WriteLine($"10 - 4 = {difference}");

Run the application:

bash
cd CalcApp
dotnet run

Output:

5 + 3 = 8
10 - 4 = 6

Example 3: Creating a Simple Web API

Let's create a basic Web API:

bash
dotnet new webapi -n SimpleApi
cd SimpleApi

Examine the generated code, then run the API:

bash
dotnet run

You'll see output showing the API is running, typically at https://localhost:5001 and http://localhost:5000. You can access the Swagger UI for the API by navigating to https://localhost:5001/swagger in your browser.

Advanced .NET CLI Features

Global Tools

The .NET CLI supports global tools, which are special NuGet packages that can be installed and run from any directory:

bash
dotnet tool install -g dotnet-ef

This installs the Entity Framework Core command-line tools globally.

Creating Custom Templates

You can create your own templates for use with dotnet new:

bash
dotnet new -i <PATH_TO_YOUR_TEMPLATE>

Common Workflows with .NET CLI

Testing

Create a test project:

bash
dotnet new xunit -n MyTests

Run tests:

bash
dotnet test

Continuous Integration

In CI environments, you often need to build and test without user interaction:

bash
dotnet build --no-incremental --no-restore
dotnet test --no-build

Summary

The .NET CLI is a powerful, cross-platform tool that simplifies the development process for .NET applications. It allows you to:

  • Create new projects from templates
  • Build, run, and publish applications
  • Manage solutions and projects
  • Work with NuGet packages
  • And much more!

Learning to use the CLI effectively can significantly improve your productivity as a .NET developer, especially when working in environments where a full IDE is not available or when automating build processes.

Additional Resources

Exercises

  1. Create a new console application and modify it to accept command-line arguments.
  2. Create a solution with three projects: a class library, a console app, and a test project.
  3. Install a popular NuGet package (like Newtonsoft.Json) and use it in your application.
  4. Create a web API and add a new controller with custom endpoints.
  5. Experiment with different build configurations (Debug vs Release) and observe the differences in output.


If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)