Introduction
A Dynamic Link library (DLL) is a library that contains functions and codes that can be used by more than one program at a time. Once we have created a DLL file, we can use it in many applications. The only thing we need to do is to add the reference/import the DLL File. Both DLL and .exe files are executable program modules but the difference is that we cannot execute DLL files directly.
Creating DLL File
Step 1. Open Visual Studio then select "File" -> "New" -> "Project..." then select "Visual C#" -> "Class library".
(I give it the name "Calculation".)
Step 2. Change the class name ("class1.cs") to "calculate.cs".
Step 3. In the calculate class, write methods for the addition and subtraction of two integers (for example purposes).
Step 4. Build the solution (F6). If the build is successful then you will see a "calculation.dll" file in the "bin/debug" directory of your project.
We have created our DLL file. Now we will use it in another application.
Using DLL File
Step 1. Open Visual Studio then select "File" -> "New" -> "Project..." then select "Visual C#" -> "Windows Forms application".
Step 2. Design the form as in the following image:
Step 3. Add a reference for the dll file, "calculation.dll", that we created earlier. Right-click on the project and then click on "Add reference".
Step 4. Select the DLL file and add it to the project.
After adding the file, you will see that the calculation namespace has been added (in references) as in the following:
Step 5. Add the namespace ("using calculation;") as in the following:
Step 6
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Calculation;
namespace MiniCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
calculate cal = new calculate();
//Addition Button click event
private void button1_Click(object sender, EventArgs e)
{
try
{
//storing the result in int i
int i = cal.Add(int.Parse(txtFirstNo.Text), int.Parse(txtSecNo.Text));
txtResult.Text = i.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Subtraction button click event
private void button2_Click(object sender, EventArgs e)
{
Try
{
//storing the result in int i
int i = cal.Sub(int.Parse(txtFirstNo.Text), int.Parse(txtSecNo.Text));
txtResult.Text = i.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

Bhuwan JoshiPosted Nov 22, 2022, 4:45 AM EST
Does using.system.collection.generic comes under class library???
reza taghaviPosted Nov 8, 2021, 1:14 AM EST
Thanks. that was very good but i have problem when i add class visual for example make my own text box then i cant use it in another project in toolBox even though another package when added to my project i can see in my toolbox
Xtian CruzPosted Oct 13, 2021, 4:11 AM EDT
This was helpful! Thanks a bunch!
Shia RissPosted Sep 21, 2020, 10:20 AM EDT
Step 6 has no instructions i get stuck
Brad WoodyPosted Jul 3, 2020, 7:13 AM EDT
Thanks! This was the perfect level of granularity - it jump started my efforts without bogging me down with too much minutia...
Damian GriffithsPosted Jun 18, 2020, 8:53 AM EDT
Very good - easy to follow - my first C# app !
Timmy MosuwosPosted May 20, 2019, 1:27 AM EDT
Very Great article !
Sufyan Raza MuhammadPosted May 11, 2019, 12:55 AM EDT
Great article !! are there any more articles depicting some advanced stuff related to this topic ?
Mano176Posted Mar 14, 2019, 1:00 PM EDT
Are you able to see your functions in Dependency Walker or DLL Export Viewer? Everything works fine when I use the DLL for another C# program, but when I want to look up the functions in the Dependency Walker or DLL Export Viewer it just seems empty. Also JNA for Java can't find the functions
Sheikh SaifPosted Feb 15, 2019, 3:28 AM EST
Bro dll file has been created but when adding to my other application as a reference it doesnt contain the function i created in my library file.. what to do?
Ramesh PasupuletiPosted Jan 4, 2019, 7:44 AM EST
How can we debug our DLL project ,which has been created with help of break points.
Kyaw NyiPosted Dec 9, 2018, 6:33 PM EST
Fastboot process how to
Rajesh KumarPosted Nov 4, 2018, 6:51 PM EST
Nice article. Thanks for sharing.................
Vishwa ShehanPosted Aug 8, 2018, 7:56 AM EDT
Hey, i'am making image editing software i am a beginner to these coding thins too but i'am hoping to add imageprocesser.dll to my image editor for filters but i really can't understand how to use it there are no any tutorials for that library so if u can help me i'll appriciate it http://imageprocessor.org/imageprocessor/imagefactory/
Anjali BhattPosted Jul 11, 2018, 10:02 PM EDT
Hii.. It's working but if we return DTO type from function so how can we access it main program. I am returning Dictionary which contain DTO but when i access it main program it's getting error in case of iterating.
mayank tyagiPosted Mar 21, 2018, 11:14 PM EDT
You can add convert.int32 instead of int.parse
Narendrakumar SPosted Jan 21, 2018, 11:45 PM EST
Hi, I am getting "Object Reference not set to an instance of an object" while calling a method in DLL from Console app. Please help
Pratik SoniPosted Nov 23, 2017, 11:23 PM EST
Simple & effective! Thanks.
basab aichPosted Feb 25, 2017, 3:29 AM EST
This is still not working in VS 2015 c#. the method "Add" & "Sub" is showing error while coding in the form.
Arvind VishwakarmaPosted Oct 4, 2014, 1:23 AM EDT
nice article