Dynamic Link Library (DLL) in C#
A Dynamic Link Library (DLL) is a file that contains reusable code and resources that multiple
applications can use. In C#, DLLs are widely used to promote modularity and code reuse. Instead
of writing the same code again in different projects, you can create a DLL and reference it
wherever needed.
1. Creation of DLL in C#:
You create a Class Library project in Visual Studio. This generates a .dll file instead of an .exe.
It contains classes, methods, and logic that can be accessed by other projects.
2. Using a DLL:
To use a DLL, you add it as a reference in your project. After referencing, you can access the
classes and methods just like they are part of your own code.
3. Advantages:
- Code reusability
- Easier maintenance
- Modular application development
- Smaller executable files
4. Example Structure:
- DLL Project: Contains a class with a method Add (int a, int b)
- Main App: References the DLL and calls Add (5, 3) to get result
5. Compilation:
Compiling a DLL doesn’t run it — it simply builds the reusable code library.
Creating a DLL in C#:
To create a DLL:
1. Open Visual Studio.
2. Create a new project and choose Class Library (.NET Framework) or Class Library (.NET
Core) depending on your target.
3. Write your classes and methods.
4. Build the project — Visual Studio will generate a .dll file in the bin/Debug or bin/Release
folder.
Using a DLL in a C# Project:
1. Right-click on the consuming project > Add Reference.
2. Browse and select your DLL file.
3. Use using Namespace Name; to access the classes and methods.
4. Now you can create objects and call methods from the DLL just like local code
Benefits of Using DLLs:
- Modular Development: You can split your codebase into logical parts.
- Code Reusability: Same DLL can be reused across multiple projects.
- Easier Updates: You can update just the DLL without changing the whole application.
- Reduced Size: The executable remains smaller since common code is moved to the DLL.
Threads and Synchronization in C#
1. Threads in C#:
A thread is the smallest unit of execution within a process. C# supports multithreading, allowing
programs to perform multiple tasks at once. You can create threads using the System. Threading
namespace.
Example of Creating a Thread:
csharp
using System;
using System. Threading;
class Program
{
static void Print Numbers ()
{
for (int i = 1; i <= 5; i++)
{
Console. WriteLine(i);
Thread. Sleep(5);
}
}
static void Main ()
{
Thread = new Thread (PrintNumbers);
Start();
}
}
2. Synchronization in C#:
When multiple threads access shared resources, there's a risk of data inconsistency.
Synchronization ensures that only one thread can access the critical section of code at a time.
Common synchronization techniques:
- Lock: Prevents multiple threads from entering a critical section simultaneously.
csharp
class Counter
{
private int count = 0;
private object lock Obj = new object();
public void Increment ()
{
lock (lockObj)
{
count++;
}
}
}
Monitor: Similar to lock but provides more control (like Wait and Pulse).
Mutex and Semaphore: Used across processes or when more complex control is needed.