.NET Core Command-Line Interface

The .NET Core Command-Line Interface (CLI) is a toolset I use constantly as a developer. Since I primarily work on .NET applications using a MacBook, I heavily rely on the dotnet commands in the Visual Studio Code terminal to manage my projects. From creating new applications to running tests and managing dependencies, the CLI has become an essential part of my workflow. It’s lightweight, versatile, and allows me to accomplish almost everything without needing a full IDE, making it ideal for cross-platform development on Windows, macOS, and Linux.

Let’s dive into some of the key features and commands I use daily with the .NET Core CLI:

Project Initialization

The first thing I often do is create a new project using the CLI. This is where it all begins, whether I’m working on a quick prototype or starting a full-scale application. Here’s an example of how I initialize a new project:

Example:

Bash
dotnet new console -n MyConsoleApp
  • dotnet new: Creates a new project or file structure.
  • console: Specifies the type of project (in this case, a console application).
  • -n MyConsoleApp: Sets the name of the project to MyConsoleApp.

The above command generates the folder and basic file structure for my project. From there, I can dive straight into coding.

Project Management

Once my project is set up, managing dependencies and configurations becomes essential. I often add or remove NuGet packages directly from the command line:


Adding a Package:

Bash
dotnet add package FluentValidation

This command installs the FluentValidation NuGet package and updates the project’s .csproj file automatically. 

Removing a Package:

Bash
dotnet remove package FluentValidation

If I decide to switch packages or clean up unused dependencies, this command comes in handy.

Building Projects

Before testing or deploying my code, I build the project to make sure everything compiles correctly. This is as simple as running:

Build Command:

Bash
dotnet build

This checks for syntax errors, compiles the code, and outputs the binaries into the bin directory. If something goes wrong, the CLI gives me detailed error messages to debug quickly.

Running Applications

When it’s time to see my code in action, I use the dotnet run command. It combines the build and execution steps into one:

Run Command:

Bash
dotnet run

This runs the application and lets me test its behavior directly from the command line. It’s super convenient for quick feedback during development.

Testing

Testing is a crucial part of my workflow, and the CLI makes it easy to run unit tests. After setting up a test project, I can run all my tests with:

Test Command:

Bash
dotnet test

This executes all the test cases in the project and outputs the results in the terminal. It’s great for spotting failing tests and fixing bugs on the spot.

Managing SDKs and Runtimes

Since I work on multiple projects that often target different versions of .NET, managing SDKs and runtimes is something I do often. With the CLI, I can list all the installed versions using:

Listing SDKs:

Bash
dotnet --list-sdks

Listing Runtimes:

Bash
dotnet --list-runtimes

If I need to target a specific version for a project, I can adjust the global.json file or install/uninstall versions as needed.

To end, the .NET Core CLI is an everyday tool for me, and it makes developing .NET applications feel seamless, especially when working across platforms like macOS. I’ve shared the commands that I find most useful and rely on heavily in my projects, and I hope they’ll be just as helpful to you. Of course, there’s so much more you can do with the CLI, and I’m always discovering new tricks myself.

Assi Arai
Assi Arai
Articles: 31