Skip to Content
Course content

78: Building Class Libraries

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Think of a class library like a professional-grade tool chest. If you're a master carpenter, you don't build a new hammer or a new spirit level every time you start a new project at a different house. That would be insane. Instead, you have a curated chest of tools that you've perfected over years. When you move from a kitchen remodel to a deck build, you just bring the chest with you. You don't care where the tools were manufactured; you just care that the hammer drives the nail exactly the way it always does.

In C#, a Class Library is that tool chest. It's a project that compiles into a .dll (Dynamic Link Library) rather than an .exe. You aren't building a program that "runs" on its own; you're building a collection of logic—classes, interfaces, and methods—that other programs can plug into. Here is how that maps to the code:

  • The Tool Chest is your Class Library project. It's the container that holds everything.
  • The Tools are your public classes and methods. These are the functions you want the "outside world" to be able to use.
  • The Manufacturing Process is the compilation. When you build the project, .NET packages it into a DLL file.
  • The Job Site is your actual Application (like a Console app or a Web API) that references that DLL to get work done.

Packaging Your Logic into a DLL

I've seen a lot of developers make the mistake of putting every single piece of logic inside their main application project. It works fine when the project is small, but once you want to share that logic—say, you have a calculation engine that needs to be used by both a desktop app and a mobile app—you're stuck copying and pasting code. That's a maintenance nightmare.

To do this right, you create a project using the "Class Library" template. The most important thing to remember here is the public keyword. Since the library is intended to be used by an external assembly, any class or method you want to expose must be marked as public. If you leave it as internal (the default for some settings), your main application won't even see it, and you'll be staring at a compiler error wondering why your code is "invisible."

// Inside my "FinancialCalculations" Class Library
namespace FinancialLib
{
    public class InterestCalculator
    {
        // We make this public so the App project can call it
        public decimal CalculateCompoundInterest(decimal principal, decimal rate, int years)
        {
            return principal * (decimal)Math.Pow((double)(1 + rate), years);
        }
    }
}

Plugging the Library into an App

Once you've built your library, it just sits there as a .dll file. It doesn't do anything until an executable project "references" it. In your IDE, this is usually as simple as right-clicking your App's dependencies and adding a Project Reference to your library.

Once the reference is set, the library's namespace becomes available to your app. I always recommend keeping your library namespaces clean and descriptive. If your library is called FinancialLib, your app code looks like this:

using FinancialLib; // Bringing the tool chest into the room

var calc = new InterestCalculator();
var result = calc.CalculateCompoundInterest(1000, 0.05m, 5);
Console.WriteLine($"Investment grew to: {result}");

The beauty of this is decoupling. You can completely rewrite the internal logic of CalculateCompoundInterest—maybe you find a more accurate formula—and as long as you don't change the method signature (the name and the parameters), you don't have to change a single line of code in your main application. You just rebuild the library, and the app automatically uses the updated "tool."




📋 Practical Task

The Multi-Project Validation Engine

Your goal is to separate "business logic" from "user interface" by creating a reusable validation library.

  1. Create the Library: Create a new C# Class Library project named ValidationCore.
  2. Implement the Logic: Inside this library, create a public class called InputValidator. Add two public methods:
    • bool IsValidEmail(string email): Returns true if the string contains both an '@' and a '.'.
    • bool IsStrongPassword(string password): Returns true if the password is at least 8 characters long and contains at least one digit.
  3. Create the Consumer: Create a separate Console Application project in the same solution named UserRegistrationApp.
  4. Link them: Add a project reference from UserRegistrationApp to ValidationCore.
  5. Execute: In the Console App, prompt the user to enter an email and a password. Use the InputValidator class from your library to check the inputs and print whether they are valid or invalid.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.