How to create a dotnet core class library using dotnet cli

NET core command-line interface (dotnet-cli) is a new NET Core toolset (commands) for developing NET Core Applications. The dotnet-cli toolset provides a list of pre-installed dotnet core project templates from which the user can create various NET Core Applications such as Console application, Web application, WPF (Windows Presentation Foundation) application, Windows Form application, or dotnet core class library. The application created with dotnet-cli can be executed on any Windows, Linux, or macOS operating system.

How to create “dotnet core class library project”

The user can use the “dotnet new classlib” or dotnet new “Class Library” command from the dotnet-CLI toolset to create a new dotnet core class library project also known as Dynamic Link Library or DLL.

The command provides few options that can be applied as a parameter to tweak the output of the command, like for example the command “dotnet new classlib –language F#” can be used to create a dotnet core class library project targetting F# programming language. Please follow through to read about a few “dotnet new classlib” command usage.

Prerequisite:

NET Core SDK 1.x and above. The current version is 5.0 and can be downloaded here.

Command Syntax

The syntax of the command is as follows: dotnet new classlib [options]

The details of all the available command options can be listed using the –help option as follows:

C:\>dotnet new classlib --help
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.


Class library (C#)
Author: Microsoft
Description: A project for creating a class library that targets .NET Standard or .NET Core
Options:
  -f|--framework  The target framework for the project.
                      netcoreapp2.1     - Target netcoreapp2.1
                      netstandard2.0    - Target netstandard2.0
                  Default: netstandard2.0

  --no-restore    If specified, skips the automatic restore of the project on create.
                  bool - Optional
                  Default: false / (*) true


* Indicates the value used if the switch is provided without a value.

Command Usage: dotnet new classlib

1. “dotnet new classlib”

The command creates a new “NET Core Class Library” project. The name of the library is not specified in the command so by default the project will be created by the name of the directory where the command was executed, additionally, the command also restores the dependency required by the project implicitly. The default language is C#, hence the .csproj file is created.

dotnet new classlib
C:\temp>dotnet new classlib
The template "Class library" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on C:\temp\temp.csproj...
  Restoring packages for C:\temp\temp.csproj...
  Generating MSBuild file C:\temp\obj\temp.csproj.nuget.g.props.
  Generating MSBuild file C:\temp\obj\temp.csproj.nuget.g.targets.
  Restore completed in 132.37 ms for C:\temp\temp.csproj.

Restore succeeded.

2. “dotnet new classlib –name MyLibrary”

The command creates a “MyLibrary” directory only if doesn’t exist and then creates the “NET Core Class Libary” project with the name of “MyLibrary.csproj” in it, additionally, the command restores the dependencies required for the project.

C:\>dotnet new classlib --name MyLibrary
The template "Class library" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on MyLibrary\MyLibrary.csproj...
  Restoring packages for C:\MyLibrary\MyLibrary.csproj...
  Generating MSBuild file C:\MyLibrary\obj\MyLibrary.csproj.nuget.g.props.
  Generating MSBuild file C:\MyLibrary\obj\MyLibrary.csproj.nuget.g.targets.
  Restore completed in 112.12 ms for C:\MyLibrary\MyLibrary.csproj.

Restore succeeded.

3. “dotnet new classlib –name MyLibrary –no-restore”

The command creates a “MyLibrary” directory only if doesn’t exist and then creates the “NET Core Class Libary” project with the name of “MyLibrary.csproj” in it. The command does not perform any restoration of dependencies and only create bare minimum file required for the project viz: MyLibrary.csproj and Class1.cs

C:\>dotnet new classlib --name MyLibrary --no-restore
The template "Class library" was created successfully.

C:\MyLibrary>dir
 Volume in drive C is Windows
 Volume Serial Number is 945D-A84B
 Directory of C:\MyLibrary

06/13/2019  09:49 PM    <DIR>          .
06/13/2019  09:49 PM    <DIR>          ..
06/13/2019  09:49 PM                86 Class1.cs
06/13/2019  09:49 PM               145 MyLibrary.csproj
               2 File(s)            231 bytes
               2 Dir(s)  43,011,649,536 bytes free

4. “dotnet new classlib –name MyLibrary –framework netcoreapp2.1”

The command creates a “MyLibrary” directory only if doesn’t exist and then creates the “NET Core Class Libary” project targetting NET core framework 2.1 with the name of “MyLibrary.csproj” in it, additionally, the command restores the dependencies required for the project. The targeted framework of the library can be verified by looking at the content of the MyLibrary.csproj file.

C:\>dotnet new classlib --name MyLibrary --framework netcoreapp2.1
The template "Class library" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on MyLibrary\MyLibrary.csproj...
  Restoring packages for C:\MyLibrary\MyLibrary.csproj...
  Generating MSBuild file C:\MyLibrary\obj\MyLibrary.csproj.nuget.g.props.
  Generating MSBuild file C:\MyLibrary\obj\MyLibrary.csproj.nuget.g.targets.
  Restore completed in 175.08 ms for C:\MyLibrary\MyLibrary.csproj.

Restore succeeded.

Content MyLibrary.csproj file:

C:\>type MyLibrary\MyLibrary.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
</Project>

5. “dotnet new classlib –name MyLibrary –language F#”

The command creates a “MyLibrary” directory only if doesn’t exist and then creates the “NET Core Class Libary” project with the name of “MyLibrary.fsproj”, the language of the project is F# and the extension of the project file will of .fsproj instead of .csproj which the default project type, additionally the command restores the dependencies required for the project.

C:\>dotnet new classlib --name MyLibrary --language F#
The template "Class library" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on MyLibrary\MyLibrary.fsproj...
  Restoring packages for C:\MyLibrary\MyLibrary.fsproj...
  Generating MSBuild file C:\MyLibrary\obj\MyLibrary.fsproj.nuget.g.props.
  Generating MSBuild file C:\MyLibrary\obj\MyLibrary.fsproj.nuget.g.targets.
  Restore completed in 121.24 ms for C:\MyLibrary\MyLibrary.fsproj.

Restore succeeded.

I hope you find the post helpful. Thanks for visiting. Cheers!!

[Further Readings: How to list dotnet core project templates using dotnet-cli | How to create a dotnet core console app using dotnet-cli |  Top 7 Web Frameworks to Learn and Focus on in 2021 |  Top 7 Programming Languages to Focus on in 2021 |  Creational Design Patterns |   Abstract Factory Design Pattern in C#  |  Factory Method Pattern in C#  |  Singleton Design Pattern in C#  |  Introduction to Design Patterns  |  How to add Git Bash to Windows Terminal Application  |  How to customize Windows Terminal Application  |  How to customize Windows Terminal Key Bindings  |  How to use Blazor EditForm for Model Validation using C#  |  How to publish a Blazor Server Application to IIS  ]

2.3 3 votes
Article Rating
Subscribe
Notify of
guest
5 Comments
oldest
newest most voted
Inline Feedbacks
View all comments
5
0
Would love your thoughts, please comment.x
()
x