Creating and Using DLL (Class Library) in C#

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 seelct "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);
            }
        }
    }
}

 

Up Next
    Ebook Download
    View all
    Learn
    View all