Home > DeveloperSection > Articles > Creating and Using Dll (Dynamic Link Library) in C#

Creating and Using Dll (Dynamic Link Library) in C#


C# C# 
Ratings:
0 Comment(s)
 5470  View(s)
Rate this:

Creating and Using Dll (Dynamic Link Library) in C#

Previously, we learn about CSS in CSS-3 And CSS-4. now we see how to create DLL's in C#


In this article I’m explaining how to create and use dl in C#.

1. Create Dll:

Open visual studio >> Select File >> New >> Project >> Windows >> Class Library. Select your project name and appropriate directory using browse button and click OK.

Creating and Using Dll (Dynamic Link Library) in C#

 

Project and it’s file:

In this project add two class by default Class1.cs and AssemblyInfo.cs inside properties folder. We will be concentrating only Class1.cs

Creating and Using Dll (Dynamic Link Library) in C#

 

Calculator Namespace:

When you open Class1.cs then you see Calculator Namespace. We will be referencing this Namespace in our clients to use this class methods.

using System;

 

namespace Calculator

{

    public class Class1

    {

    }

}

Now add some calculator method in your project.

using System;

 

namespace Calculator

{

    public class Class1

    {

        public static int Add(int num1,int num2)

        {

            return num1 + num2;

        }

        public static int Sub(int num1, int num2)

        {

            return num1 - num2;

        }

        public static int Mul(int num1, int num2)

        {

            return num1 * num2;

        }

        public static int Division(int num1, int num2)

        {

            return num1 / num2;

        }

    }

}

 

Now Build your project after build succeeded genrate Calculator.dll file inside bin >> Debug folder.

Creating and Using Dll (Dynamic Link Library) in C#

Use Dll:

Make a client application for use dll file.

Create console application:

Open visual studio >> Select File >> New >> Project >> Windows >> Console Application. Select your project name and appropriate directory using browse button and click OK.

Creating and Using Dll (Dynamic Link Library) in C#

 

Add reference of the Namespace:

Now add reference to the library. Project >> Add reference

Creating and Using Dll (Dynamic Link Library) in C#

Now on this page click browse button to browse your library.

Creating and Using Dll (Dynamic Link Library) in C#

Use Calculator Namespace and call method.

using Calculator;

 

namespace UseDll

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.Write("Enter Your Operator Choice  Add + Sub - Mul * Division /");

            char choice = Convert.ToChar(Console.Read());

            switch (choice)

            {

                case '+':

                    {

                        int add = Class1.Add(10, 20);

                        Console.WriteLine("Sum of the two number " + add);

                        break;

                    }

                case '-':

                    {

                        int sub = Class1.Sub(50, 20);

                        Console.WriteLine("Substrack of two integer number " + sub);

                        break;

                    }

                case '*':

                    {

                        int mul = Class1.Mul(2, 4);

                        Console.WriteLine(mul);

                        break;

                    }

                case '/':

                    {

                        int div = Class1.Division(10, 2);

                        Console.WriteLine(div);

                        break;

                    }

                default:

                    {

                        Console.WriteLine("Wrong input!!");

                        break;

                    }

            }

        }

    }

}

 

Output

Creating and Using Dll (Dynamic Link Library) in C#

 You  may also want to read: Creating C# Class Library (DLL) Using Visual Studio .NET


In my next post, we are going to learn about : Join, Sleep and Interrupt methods in C# Threading


Don't want to miss updates? Please click the below button!

Follow MindStick