C#
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented C#
-
Section 4: Working with Data
-
Section 5: Error Handling
-
Section 6: Delegates and Events
-
Section 7: Async Programming
-
Section 8: More Language Features
-
Section 9: File I/O and Serialization
-
Section 10: Networking in .NET
-
Section 11: The .NET Ecosystem
-
Section 12: Memory and Performance
-
Section 13: Concurrency Beyond Async
-
Section 14: Reflection and Attributes
-
Section 15: Testing and Best Practices
-
Section 16: Design Patterns in C#
-
Section 17: Standard Library Deep Dive
-
Section 18: Data Structures and Algorithms in C#
-
Section 19: GUI and Desktop Development Overview
-
Section 20: Practical Projects
-
Section 21: More Practice Exercises
-
Section 22: More Standard Library and Text Processing
-
Section 23: More Design Patterns
-
Section 24: More Projects
-
Section 25: Interview Practice
-
Section 26: C# Keywords Reference (Modifiers)
-
Section 27: C# Keywords Reference (Statements)
-
Section 28: C# Keywords Reference (Operators)
-
Section 29: BCL: System.Collections.Generic
-
Section 30: BCL: System.Linq
-
Section 31: BCL: System.Threading
-
Section 32: BCL: System.IO
-
Section 33: BCL: System.Text and System.Text.Json
-
Section 34: BCL: System.Net.Http
-
Section 35: C# Language Specification Topics
-
Section 36: More Practice Exercises
-
Section 37: More Async Patterns
-
Section 38: More BCL: System.Reflection and System.Diagnostics
-
Section 39: Nullable Reference Types In Depth
-
Section 40: C# Records and Pattern Matching In Depth
-
Section 41: Dependency Injection Deep Dive
-
Section 42: More Interview and Whiteboard Practice
78: Building Class Libraries
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
publicclasses 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.
- Create the Library: Create a new C# Class Library project named
ValidationCore. - 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.
- Create the Consumer: Create a separate Console Application project in the same solution named
UserRegistrationApp. - Link them: Add a project reference from
UserRegistrationApptoValidationCore. - Execute: In the Console App, prompt the user to enter an email and a password. Use the
InputValidatorclass from your library to check the inputs and print whether they are valid or invalid.
There are no comments for now.