Create Your First DLL file (C#)
For beginners, creating a simple DLL (Dynamic Link Library) in C# is a great way to learn about libraries, code reuse, and modular programming. Let’s create a straightforward DLL that includes a class with a method to say hello. This example assumes you have basic knowledge of C# and Visual Studio.
Step 1: Creating a Class Library Project in Visual Studio
- Open Visual Studio.
- Select “Create a new project”.
- In the project type selection window, search for and select “Class Library (.NET)” (ensure you choose the .NET version suitable for your needs, such as .NET Core, .NET Framework, or .NET 5/6 depending on your Visual Studio version).
- (Try: NET Framework )
- Click “Next”.
- Enter your project name, e.g., “MyFirstDLL”.
- Choose a location for your project and click “Create”.
Step 2: Writing Your First Class
By default, Visual Studio creates a class file named Class1.cs
. You can rename this file to something more descriptive, such as Greeter.cs
. To rename, right-click the file in the Solution Explorer, select “Rename”, and enter the new name.
Open Greeter.cs
and replace its contents with the following code:
using System;
namespace MyFirstDLL
{
public class Greeter
{
public string SayHello(string name)
{
return $"Hello, {name}!";
}
}
}
This code defines a Greeter
class with a method SayHello
that takes a person’s name as an argument and returns a greeting message.
Step 3: Building the DLL
- To build your project and create the DLL, go to the “Build” menu in Visual Studio and select “Build Solution”.
- After building the solution, navigate to the project folder on your file system. Inside the
bin\Debug
orbin\Release
directory (depending on your build configuration), you will find your newly created DLL,MyFirstDLL.dll
.
Step 4: Using Your DLL in Another Project
To use your DLL in another project, you need to add a reference to it:
- Create or open another project in Visual Studio where you want to use the
MyFirstDLL.dll
. - Right-click on the project in the Solution Explorer, select “Add” > “Reference…”.
- Click “Browse”, find your
MyFirstDLL.dll
file, select it, and click “Add”. - Ensure the checkbox next to your DLL under the “Projects” tab is checked, and click “OK”.
Now, you can use the Greeter
class in your project like this:
using MyFirstDLL; // Make sure to include the namespace of your DLL
class Program
{
static void Main(string[] args)
{
Greeter greeter = new Greeter();
string message = greeter.SayHello("World");
Console.WriteLine(message);
}
}
This program creates an instance of the Greeter
class from your DLL and calls the SayHello
method to print a greeting to the console.
Conclusion
Congratulations! You’ve just created and used your first DLL in C#. DLLs are powerful tools for modularizing code and facilitating code reuse across different projects. As you become more comfortable with C# and Visual Studio, you can explore more complex DLL creation, including public interfaces, classes, and various data types.