In C#, class libraries are assemblies that contain reusable code, typically in the form of classes, methods, and other types. These libraries can be used across different projects. You can build and compile class libraries from the command line without using an integrated development environment (IDE) like Visual Studio. This can be particularly useful for automation, scripting, or working on lightweight projects.
This guide will cover the steps to create, build, and reference class libraries using the command line in C#.
A class library is a collection of code organized in a DLL (Dynamic Link Library) or EXE (Executable) file. The classes, methods, and other types in the library can be accessed and used by other programs.
Key points about class libraries:
Main method).The .NET CLI (dotnet) is a powerful tool provided by Microsoft for building, running, and managing .NET applications from the command line. It allows developers to create, restore, build, run, and publish projects without needing Visual Studio.
Ensure you have the .NET SDK installed on your system. You can verify this by running:
dotnet --version
This should display the version of the .NET SDK installed.
csc)If you're working with .NET Framework instead of .NET Core or .NET 5/6, you can use the C# compiler (csc), which is included in the .NET Framework SDK, to compile class libraries.
However, the .NET CLI (dotnet) is the recommended approach for modern development.
To create a class library in C# using the .NET CLI, follow these steps:
dotnet new command to create a new class library project.dotnet new classlib -n MyClassLibrary
dotnet new classlib: Creates a new class library project.-n MyClassLibrary: Specifies the name of the project.This will create a folder named MyClassLibrary with the following default structure:
MyClassLibrary
│ MyClassLibrary.csproj
│ Class1.cs
└───obj
└───bin
The MyClassLibrary.csproj file is the project file, and Class1.cs is a sample class file.
Navigate to the Class1.cs file, and you can define your custom classes. For example:
namespace MyClassLibrary
{
public class MyClass
{
public string SayHello()
{
return "Hello from MyClassLibrary!";
}
}
}
This is a simple class that has a SayHello() method.
To build the class library, navigate to the project directory (if not already there) and run the following command:
dotnet build
This will compile the class library and generate the DLL file. The output will typically be in the bin/Debug/netstandard2.0/ directory (or the appropriate version folder, like net5.0 or net6.0).
You should see a file like MyClassLibrary.dll in the output directory.
To use your class library in another project, you can reference the generated DLL or include the class library as a dependency in another project.
You can directly reference the generated MyClassLibrary.dll in another project. To do this:
dotnet new console -n MyConsoleApp
MyConsoleApp directory, edit the .csproj file to add a reference to the class library:<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="..\MyClassLibrary\bin\Debug\net6.0\MyClassLibrary.dll" />
</ItemGroup>
</Project>
MyClassLibrary.dll.Program.cs file, you can now use the class library:using MyClassLibrary;
class Program
{
static void Main()
{
var myClass = new MyClass();
Console.WriteLine(myClass.SayHello());
}
}
dotnet run
This should display the message from the SayHello() method in your class library.
Instead of referencing the DLL directly, you can add a project reference to the class library, making it easier to manage and build.
MyConsoleApp directory, run:dotnet add reference ../MyClassLibrary/MyClassLibrary.csproj
This command adds a reference to the MyClassLibrary project directly, so the DLL is built automatically along with your application.
Once you have developed your class library, you may want to distribute it as a NuGet package or just ensure it's built correctly for production.
To build the class library in Release configuration (optimized for production), use the following command:
dotnet build --configuration Release
This will place the compiled DLL in the bin/Release/net6.0/ directory.
You can also publish the class library to a NuGet feed or store it in a local directory.
dotnet pack --configuration Release --output ./nupkgs
This command will package the class library as a .nupkg file in the nupkgs folder.
dotnet nuget push ./nupkgs/MyClassLibrary.1.0.0.nupkg --api-key <your-api-key> --source https://api.nuget.org/v3/index.json
Here’s a summary of the key commands used when building and managing class libraries from the command line in C#:
Create a new class library:
dotnet new classlib -n MyClassLibrary
Build the class library:
dotnet build
Run the class library (in a consuming app):
dotnet run
Add a project reference:
dotnet add reference <path-to-class-library>
Build in Release mode:
dotnet build --configuration Release
Create a NuGet package:
dotnet pack --configuration Release --output ./nupkgs
Publish the NuGet package:
dotnet nuget push ./nupkgs/MyClassLibrary.1.0.0.nupkg --api-key <your-api-key> --source https://api.nuget.org/v3/index.json
Building class libraries from the command line in C# is a straightforward process using the .NET CLI. You can create, build, and manage class libraries effectively without the need for a full-fledged IDE like Visual Studio. This approach is ideal for automation, continuous integration, and lightweight development scenarios. The flexibility of the .NET CLI allows you to integrate class libraries into larger projects or distribute them as packages.
Open this section to load past papers