.NET Core - When to use "dotnet new sln"

Solution 1:

Is "dotnet new sln" a new command?

Yes. In version 1.0.1 of the dotnet command line interface, there is a dotnet new sln command. The command came with the change from project.json to csproj. If we run dotnet new --help, we will see "Solution File" as one of the templates.

> dotnet new --help

Templates                 Short Name      Language      Tags
----------------------------------------------------------------------
Console Application       console         [C#], F#      Common/Console
Class library             classlib        [C#], F#      Common/Library
Unit Test Project         mstest          [C#], F#      Test/MSTest
xUnit Test Project        xunit           [C#], F#      Test/xUnit
ASP.NET Core Empty        web             [C#]          Web/Empty
ASP.NET Core Web App      mvc             [C#], F#      Web/MVC
ASP.NET Core Web API      webapi          [C#]          Web/WebAPI
Solution File             sln                           Solution

when should I use this?

Two times to use a solution file are:

  1. when we want to use Visual Studio, and/or
  2. when we want to manage several projects as a single unit.

What benefits do I gain from creating a .sln file instead of just having project files? Is it mainly for opening in Visual Studio? I use Visual Studio Code for Mac, so it may not be applicable.

One of the benefits that do not require Visual Studio is the management of multiple projects as a single unit.

For instance, on a Mac with Visual Studio Code, we can use the dotnet CLI to create a new solution, create a few projects, add those projects to the solution, restore the solution, and build the solution.

dotnet new sln --name FooBar
dotnet new console --name Foo --output Foo
dotnet new console --name Bar --output Bar
dotnet sln add .\Foo\Foo.csproj
dotnet sln add .\Bar\Bar.csproj
dotnet restore
dotnet build FooBar.sln

The last command, which calls dotnet build, has the benefit of building all the projects that are in the solution. Without a solution, we would need to call dotnet build on each project.

There are no doubt other benefits which do not require the use of Visual Studio. I leave those to you to discover.