Think of assemblies as the building blocks of .NET applications, containing all the compiled code in libraries or executable files. Don’t worry if it sounds a bit complicated—let's break it down step by step.
Step 1: Creating a Class Library
Imagine you have some code that you want to reuse across multiple projects. That’s where a class library comes in handy—it’s a project that holds reusable code. Let’s build a simple math library to get started!
1.1: Create a New Class Library
First things first, let's create the project. Open up your terminal and navigate to the directory where you want to store your project (this should be above your HelloWorld
project folder). Now, type in this command:
dotnet new classlib -o MathLib
Boom! You just created a new class library project.
1.2: Peek Inside the Project
Now that the project is set up, take a look at its structure. You’ll find files similar to what you see in a console app—there’s a .csproj
file, some .cs
files, and more.
However, since this is a library project, the .csproj
file won’t have an OutputType
that says Exe
, meaning you can't directly run this as an executable program. (But don’t worry, we’ll get to using it soon!)
1.3: Rename and Tweak the Class File
Now let's do a bit of renaming and coding. Inside the MathLib
project, you’ll find a file named Class1.cs
. Let’s rename that to something more meaningful, like MathLib.cs
.
Then, open up the file and replace its contents with this simple math class:
namespace MathLibNamespace
{
public class MathLibClass
{
public int Add(int x, int y)
{
return x + y;
}
}
}
What does this do? It creates a class that has a method to add two numbers together—super simple, right?
1.4: Build the Library
Okay, it’s time to compile (or build) your library. Run this command:
cd MathLib
dotnet build
Now, because this is a library, you can’t just run it directly. If you try, you’ll get an error like this:
A runnable project should target a runnable TFM (for instance, net5.0) and have OutputType 'Exe'.
The current OutputType is 'Library'.
Don’t worry about this—it's just telling you that a library can't be run on its own. We’ll fix that by using this library in another project!
Step 2: Using the Class Library in a Console App
Now that we’ve created our library, let’s use it! We’ll reference it in a new console application so we can actually see it in action.
2.1: Create a Console Application
Navigate back to your parent directory and create a console app. Here’s the command:
dotnet new console -o MathLibUser
cd MathLibUser
This will create a basic console app called MathLibUser
.
2.2: Link Your Class Library
To use the math library we just created, we need to add a reference to it. Let’s do that with this command:
dotnet add reference ../MathLib/MathLib.csproj
Behind the scenes, this adds a ProjectReference
in the .csproj
file of the MathLibUser
project, like this:
<ItemGroup>
<ProjectReference Include="..\MathLib\MathLib.csproj" />
</ItemGroup>
Now your console app knows about the MathLib
project.
2.3: Write the Code to Use the Library
Next, let’s use our math library! Open up Program.cs
in the MathLibUser
project and replace the existing code with this:
using MathLibNamespace;
public class Program
{
public static void Main()
{
var m = new MathLibClass();
Console.WriteLine(m.Add(2, 3));
}
}
Here’s what’s happening: we’re using the MathLibClass
from the MathLibNamespace
, and calling its Add
method to add 2 and 3 together.
2.4: Run the Console App
Finally, let’s run the console app to see it in action! Just run:
dotnet run
And voilà! You should see the output:
5
That’s it—you’ve successfully used your own library in a C# project!
Pretty neat, huh? With assemblies, you’ve got a flexible way to break down your code into reusable chunks. The more you work with them, the more you'll appreciate how useful they are for building larger and more maintainable applications.