The .NET CLI is the standard command-line tool of the .NET ecosystem. With it, you can create, run, and even publish .NET projects using only terminal commands, without the need for an IDE. In this quick guide, we will introduce you to the main features of the .NET CLI, knowledge that is currently essential for every .NET developer.
What is the .NET CLI?
Command-line tools, called CLI (Command-Line Interface), are applications that allow you to perform various tasks in a given context through terminal commands. Since they do not have a graphical interface, they are typically lightweight and provide the same user experience across different operating systems. Furthermore, because they operate based on terminal commands, they can be used not only manually but also as part of automated workflows, such as CI/CD pipelines.
Some common tools of this type among developers are apt-get, used for package management in Linux; npm and yarn, JavaScript package managers; docker and kubectl interfaces, for Docker and Kubernetes, respectively; as well as those that come with frameworks and other development tools, like ng (Angular), ionic (Ionic), and nest (Nest.js).
In this context, the .NET CLI is a cross-platform tool that simplifies the .NET development lifecycle. It can be accessed directly from the command prompt, making it accessible and easy to use across different operating systems without the dependency on an IDE.
Command Syntax
The syntax of the .NET CLI commands is simple, divided into three main parts:
dotnet command arguments
- “dotnet” is the executor, i.e., the main application.
- Next, we have the specific command, indicating which action will be performed.
- Finally, come the arguments and options, which provide the command with the necessary information for its operation. Each command expects different arguments, which can be optional.
For example, to create a new console project in C#, we use the command:
dotnet new console --language C#
In the above command:
- “dotnet” is the executor that calls the .NET CLI.
- “new” is the specific command that indicates we want to create a new project.
- “console” is an argument that specifies the type of project we’re creating, in this case, a console project.
- “–language C#” is an option that defines the programming language to be used in the project, which in this case is C#.
Some options have both a main version and a shortened version. For example, the option --help can also be passed as -h.
To check if you have the .NET CLI installed, just open a command prompt and type “dotnet.” This will display information about the installed version and the available commands.
If you don’t have .NET installed, you can get it from the official Microsoft website.
Exploring Basic Commands
Getting Help
Whenever you need help with a .NET CLI command, just add “-h” or “–help” at the end of the command to display detailed information about its usage. For example, typing “dotnet new -h” will show a list of available options and arguments for the “new” command, as shown below:
dotnet new -h
This command will display a description of the “new” command, including the different template options that can be used (such as console, mvc, classlib, etc.), as well as the options and arguments specific to each template. This functionality is extremely useful for exploring all the capabilities of the CLI and learning how to quickly use new commands and options.
Creating a Project
One of the first steps when starting a new project is creating its basic structure. With the .NET CLI, this is done in a simple and straightforward manner. For example, to create a new console project, use the command:
dotnet new console
Each command usually assumes some default values for arguments. For example, when executing the command above, a project will be created with the same name as the folder where it was run. For instance, if the terminal is open in the folder C:\MyProject, the above command will create a project called MyProject.csproj. Another default example is the language used (C#). If you want to change it, you can specify the --language or -lang option.
To see which project templates are available, use the following command:
dotnet new list
For each listed template, you can get help with the -h argument to understand which other arguments can be provided.
Managing NuGet Packages
In most of the projects we work on, we need to use NuGet packages. With the .NET CLI, you can add a package like this:
dotnet add package PackageName
This command must be executed in the folder where the .csproj file is located and adds the reference to the specified package in the project file and downloads the latest version of the NuGet package. If you need to add a specific version of a package, just specify the desired version using the --version or -v option.
To remove the package, simply use the remove command:
dotnet remove package PackageName
As the project grows, it’s common to use several packages. So, to list all the installed packages, run:
dotnet list package
This command shows a list of all the installed packages in the current project, including information about the version of the package and the source from which it was obtained. This is useful for quickly checking which dependencies are being used in your project.
Finally, to ensure all project dependencies are correctly installed and configured, use the command:
dotnet restore
This command uses NuGet.exe behind the scenes to restore all the dependencies listed in the .csproj project file. It checks for the necessary NuGet packages and downloads them to the project’s local package directory, ensuring all the required libraries and tools are available for development.
Cleaning and Building the Project
Now to keep your development environment clean and ensure that the code is compiled correctly, you can use the following commands:
dotnet clean
The dotnet clean command removes all files generated by the previous build, including binary and output files, keeping only the essential project files. This is useful when you want to start a new build from clean source code.
To compile the project, simply run:
dotnet build
The dotnet build command compiles the current .NET project and its dependencies. During the build process, it checks the syntax of the code, resolves all library references, and generates the binary files needed for the application to run. Any errors or warnings encountered during the build will be displayed in the command prompt so you can fix them before proceeding.
Running the Project
Once your project is created, dependencies restored, and successfully built, you can easily run it with the command:
dotnet run
This command not only compiles the project if needed but also runs the .NET application directly from the source code. This is useful for quickly testing implemented features or verifying the application’s behavior in different usage scenarios.
When you run the dotnet run command, the console will display the output generated by the application. For example, our console project contains simple code that prints “Hello, World!” on the screen:
Publishing the Project
To prepare your project for distribution or deployment, you can use the dotnet publish command. This command compiles the code, copies all the necessary files, and generates a package that can be easily deployed on a server or shared with other developers. For example:
dotnet publish -c Release -o outputFolder
The -c Release option specifies that the build should be done in Release mode, which is optimized for distribution. The -o outputFolder option defines the output directory where the published files will be placed. This way, the project will be compiled, and the necessary files will be copied to the specified folder, ready for deployment.
Conclusion
In this article, we explored the essential features of the .NET Core CLI, a powerful tool for .NET developers who want to maximize their efficiency and productivity. Mastering the CLI not only simplifies everyday tasks such as project creation and compilation but also prepares you to adopt advanced practices like CI/CD and build automation.