Search⌘ K
AI Features

Creating a Class Library

Explore the process of creating a C# class library project without an entry point and learn how to reference it in a console application for code reuse. Understand using namespaces, setting up Visual Studio solutions, and utilizing .NET CLI commands for building, running, and managing dependencies. Discover basic unit testing with xUnit and troubleshooting tips to maintain smooth development workflow.

Class library projects do not contain a Main method because a library is not an executable. It cannot run on its own but can be referenced and used inside other projects.

Let’s create and use a class library in Visual Studio.

First, we create a console application that serves as our executable. After creation, the environment looks similar to the following:

Console application template
Console application template

Right-click the “Solution” node in the “Solution Explorer” and click “Add” and then “New Project”.

Adding a new project
Adding a new project

Select “Class library” as the project type and complete the setup wizard.

Our solution now contains two projects.

Solution Explorer
Solution Explorer

Next, we replace the default code in the class library with an Employee class.

C# 14.0
namespace EmployeeDirectory;
public class Employee
{
public string FirstName;
public string LastName;
public Employee(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public void PrintFullName()
{
Console.WriteLine($"{FirstName} {LastName}");
}
}
  • Line 1: ...