10 commands you don't want to be without in .Net Core
Tags: dotnet, tutorial, webdev, beginners
Published: 2019-09-21 21:23:38

---
title: 10 commands you don't want to be without in .Net Core
published: true
description: This describes 10 essential commands when using .Net Core
tags: dotnet, tutorial, webdev, beginners
cover_image: https://thepracticaldev.s3.amazonaws.com/i/wrxdtem3goz8clflbjeo.jpg
canonical_url: https://softchris.github.io/pages/dotnet-10-commands.html
---

Follow me on [Twitter](https://twitter.com/chris_noring), happy to take your suggestions on topics or improvements /Chris

TLDR; this is an article describing 10 great commands in .Net Core CLI, hopefully, there's something for everybody here, from absolute beginner to more seasoned developer.

I come from a long background of doing .Net. One of the best parts of it was Visual Studio, an absolutely outstanding IDE that allowed you to easily write code with code completion, easy debugging and packaging of your apps. Then, years later after I  first started with .Net came .Net Core and with it the ability to use .Net cross-platform. With .Net Core came also a very capable CLI tool. Using the old .Net you knew there was an MSBuild command in the background but you seldom needed to care, Visual Studio handled it all for you. 

Using .Net Core though you have the best of both worlds, still the first-class experience of either Visual Studio or the amazing Visual Studio Code AND with the CLI tool we have a first-rate terminal experience as well.

> So why use terminal commands, I'm a UI guy?

I know many of you are thinking like this (I used to as well) and the answer is that there is more than one reason. 

- **Scripting** is a very good reason, scripting allows us to easily set up CI/CD
- **Speed** is one factor, working with the terminal is usually faster than using a visual environment. 
- **Choose your own editor**, it also allows you to use the favorite editor of your choice. We all have our favorite editor. .Net used to be very tightly coupled to Visual Studio, now with .Net Core CLI you can quite easily build, test, package, publish your projects through the terminal.

Regardless of the reason you find the most compelling, it's good to know what commands are run underneath and get some insights on what they do for you. So let's look at the 10 most important commands using the .Net Core CLI:  

## -1- dotnet new
This command helps you scaffold a project. So usually you call it like this:

```
dotnet new <type of project template> - o <name of directory>
```

A real example might look like this:

```
dotnet new console -o app
```

The above will create a console project in the directory `app`. There's a lot more to this command so have a look at the docs page

> [dotnet new command](https://docs.microsoft.com/dotnet/core/tools/dotnet-new?tabs=netcore22&?wt.mc_id=devto-blog-chnoring)

## -2- dotnet restore
This restores the dependencies of a project. What does that mean exactly? Well, when you have added NuGet packages to your solution that adds a reference to your project file, your `.csproj`. If you check out the project from GitHub those packages are maybe not versioned and will need to be added to your project. Running `restore` at this point will fetch the packages from the NuGet repository. 

The command can be run in two ways:
- **Explicitly**, you type it and it fetches the packages you need
- **Implicitly**, it runs as part of another command being executed

Here's a full list of commands that runs `restore` implicitly:

- dotnet new
- dotnet build
- dotnet build-server
- dotnet run
- dotnet test
- dotnet publish
- dotnet pack

To learn more about this command, read more here:

> [dotnet restore command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-restore?tabs=netcore2x&wt.mc_id=devto-blog-chnoring)

## -3- dotnet build

This command builds a project and all of its dependencies. If you are at the root directory and you have a solution, then it will build the whole solution, so all projects within the solution. If you are in a specific project directory then it will only build that directory, the *where* matters. There's a lot more to the `build` command than that so have a look at some more details at the following link:

> [dotnet build command](https://docs.microsoft.com/dotnet/core/tools/dotnet-build?wt.mc_id=devto-blog-chnoring) 

## -4- dotnet run

The `run` command is what you use to execute your code. 

To use it simply place yourself in the project directory of the project you want to run and type:

```
dotnet run
```


NOTE, It's used in the context of projects, not built assemblies. If you're trying to run a framework-dependent application DLL instead, you must use `dotnet` without a command. For example, to run myapp.dll, use:

```
dotnet myapp.dll
```

To read more about this command check out the following link:

> [dotnet run command](https://docs.microsoft.com/dotnet/core/tools/dotnet-run?tabs=netcore21&wt.mc_id=devto-blog-chnoring)


## -5- dotnet test

This command is what you use to execute the tests of a test project. This comes with quite a few arguments which enable you to run specific tests or the whole test suite.

To read more about this command, have a look at the following link:

> [dotnet test command](https://docs.microsoft.com/dotnet/core/tools/dotnet-test?tabs=netcore21&wt.mc_id=devto-blog-chnoring)

if you are completely new to testing, check out this article I wrote on getting started with testing:

> https://dev.to/dotnet/testing-in-net-core-ojh


## -6- dotnet pack

Now we are getting into an interesting area namely creating your own NuGet packages. You create a package with this command and you can have packages in a local repository as well as the official global one. To learn more about this have look at the official docs page:

> [dotnet pack command](https://docs.microsoft.com/dotnet/core/tools/dotnet-pack?wt.mc_id=devto-blog-chnoring)

Also, check out an article I wrote about how to do this from scratch and learn how to publish your package so someone else can download it:

> [Creating your first package in NuGet](https://dev.to/dotnet/how-you-can-create-reusable-packages-for-net-core-using-vs-code-c-and-nuget-3kn9)


## -7- dotnet clean

This command cleans the output of a project. This means it takes away both the contents of obj as well as bin folders. This command comes with a few arguments so you can for example choose to only clean a specific runtime or framework. Read more about this command at the official docs:

> [dotnet clean command](https://docs.microsoft.com/dotnet/core/tools/dotnet-clean?wt.mc_id=devto-blog-chnoring)

## -8- dotnet sln

This is the command you use to manage your solution. If you are completely new to dotnet, a solution keeps track of many projects and it's great way of managing a group of different projects that logically belongs together to do things like building or maybe publish an executable. Because this command manages everything around solutions it's good to know how to start out.

To create a solution you just place yourself in a directory of your choosing and run 

```
dotnet new sln
```

This will create a solution file with the same name as the directory. For example:

```
cd hello
dotnet new sln
```

This will create a `hello.sln` file. To manage the solution you have the following commands at your disposal:

- **add**, this will add a project to your solution. 
- **remove**, this will remove a project from your solution
- **list**, this lists all the projects in a solution

To learn more about this command, check out the official docs page:

> [dotnet sln command](https://docs.microsoft.com/dotnet/core/tools/dotnet-sln?wt.mc_id=devto-blog-chnoring)

Additionally, have a look at the blog post I wrote teaching you how to create solutions, projects and how to start out with .Net Core

> [Starting out with .Net Core](https://dev.to/dotnet/how-you-can-get-started-with-net-core-and-c-in-vs-code-30gc) 


## -9- dotnet add/remove reference

This command will add a project to your project. The purpose is for example for your project to use code from another project. For that to be possible, you first need to add a reference to that project before the compiler and your IDE will recognize it. For example, you are currently developing the console project `app` and you want to add the project `lib` to `app`. You can do this in one of two ways:

1. **Add a reference from solution dir**,  if you are standing in the solution directory (one level above the app directory) then you type `dotnet add app/app.csproj reference lib/lib.csproj`
2. **Add a reference from the current directory**, then you would type `dotnet add reference lib1/lib1.csproj`

To learn more about this command, check out the docs page:

> [dotnet add reference command](https://docs.microsoft.com/dotnet/core/tools/dotnet-add-reference?wt.mc_id=devto-blog-chnoring)


## -10- dotnet add/remove package

Now we are agin talking about NuGet packages. The `add` command allows us to add a package from NuGet by specifying it by name:

```
dotnet add package Newtonsoft.Json
```

This will add `Newtonsoft.Json` package to our project file. It will also run a `dotnet restore` command which will fetch the package from NuGet's repository

To learn more about this command, have a look at this link:

> [dotnet add package command](https://docs.microsoft.com/dotnet/core/tools/dotnet-add-package?wt.mc_id=devto-blog-chnoring)


## References

Here are some references you might need:

- [install dotnet](https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install)
This will take you to the install page for .Net Core. While you are there, check out [https://dotnet.microsoft.com](https://dotnet.microsoft.com). That's a great site for anything .Net, full of tutorials.

- [.Net Core Series](https://dev.to/azure/net-core-series-serverless-graphql-nuget-testing-entity-framework-and-more-5dgk)
Have a look at this series of .Net Core articles I wrote, everything from your first steps in .Net Core, to Serverless and even GraphQL