Create Class Library in .NET Core Beginner to Advanced Guide
A class library is a project that contains reusable code such as functions, methods and classes that can be shared across multiple applications. Class libraries encapsulate logic, making your codebase modular, maintainable and easier to test. In this article, we will explore how to create class library in .NET core application. After reading this article, you will be able to create class library easily.
Why should we use a Class Library
- Code Reusability: Avoid duplicating logic across multiple projects. For example, if you create a logging utility or data access layer in a class library, any application can consume it just by referencing the DLL.
- Maintainability: Changing in library will automatically reflect in all dependent projects so that any bug fix or enhancement made in the library instantly benefits all applications using it.
- Separation of Concerns: It allows you to keep your business logic, data access logic and utility methods isolated and well-organized.
Prerequisites
Before you start, make sure the following tools are installed
- .NET SDK installed Download here
- Visual Studio OR Visual Studio Code
- Basic knowledge of C# and .NET Core
Method 1 : Using the .NET CLI
Step 1: Create the Class Library Project
- Open a terminal or command prompt
- Navigate to the directory/folder where you want to create the project.
- Run the following command to create a new class library
dotnet new classlib -n <LibraryName>
This will generate a project folder named <LibraryName> with necessary files including: <LibraryName>.csproj (project file) and a default class file (Class1.cs).
Step 2: Navigate into the Project Directory
cd <LibraryName>
Step 3: Build the Class Library
dotnet build
This command compiles the project and generates a .dll file in the bin directory. The .dll file is the compiled library that you can reference in other projects
Step 4: Reference the Class Library in Another Project
- Navigate to the directory of the project/application that will use this library
- Add the reference of class library
dotnet add reference ../<LibraryName>/<LibraryName>.csproj
- Run the build command in the main project directory to ensure the reference is correctly added
dotnet build
Method 2: Using Visual Studio
Create a New Class Library Project
- Open Visual Studio
- Click on "Create a new project"
- Select "Class Library" and click Next.
- Provide a name for your class library and click Create.
Comparison Table of Creating Class Library
Feature | CLI | Visual Studio |
---|---|---|
Create Project | dotnet new classlib |
GUI-based "Create a Project" flow |
Add Reference | dotnet add reference |
Right-click > Add Reference |
Build Library | dotnet build |
Ctrl + Shift + B |
Output Format | .dll file | .dll file |
Testing the Class Library in .NET Core
To ensure your class library is reliable, maintainable and bug-free, it’s essential to write automated unit tests. Unit testing validates your logic against a variety of inputs and edge cases, helping you catch errors early and improve confidence during refactoring or updates.
Why Unit Test a Class Library?
- Verify Logic: Ensure each method behaves as expected for different scenarios.
- Prevent Regressions: Catch bugs introduced by future code changes.
- Support CI/CD Pipelines: Integrate tests into your DevOps workflow for automation.
- Encourage Clean Design: Writing testable code naturally leads to better abstraction and separation of concerns.
How to Test a Class Library in .NET Core
1: Create a Unit Test Project
dotnet new xunit -n MyLibrary.Tests
2: Add a Reference to Your Class Library
dotnet add reference ../<LibraryName>/<LibraryName>.csproj
3: Write a Sample Unit Test
Here’s an example assuming your class library has a Calculator class:
Library Code
namespace MyLibrary { public class Calculator { public int Add(int x, int y) => x + y; public int Divide(int x, int y) => y == 0 ? throw new DivideByZeroException() : x / y; } }
Unit Test Code
using MyLibrary; using Xunit; namespace MyLibrary.Tests { public class CalculatorTests { [Fact] public void Add_ShouldReturnCorrectSum() { var calc = new Calculator(); var result = calc.Add(3, 4); Assert.Equal(7, result); } [Fact] public void Divide_ByZero_ShouldThrowException() { var calc = new Calculator(); Assert.Throws<DivideByZeroException>(() => calc.Divide(10, 0)); } } }
4: Run Your Tests
dotnet test
Tips for Writing Good Unit Tests
Best Practice | Why It Matters |
---|---|
Use clear naming | Improves readability and intention (e.g., MethodName_State_Result ) |
Test edge cases | Avoids runtime surprises |
Keep tests independent | No shared state means more reliable tests |
Cover negative paths | Test for exceptions, invalid inputs, etc. |
Aim for fast execution | Avoid slow dependencies (e.g., DB, network) |
Testing your .NET Core class library ensures long-term quality and maintainability. Whether you're using xUnit, NUnit, or MSTest, writing robust unit tests helps you
- Build confidence in your logic
- Refactor without fear
- Catch bugs early
- Write better code