How To Create And Use DLLs (Class Libraries) In ASP.NET

Introduction

A DLL is a library that includes functions and codes that may be utilized by several programs at the same time. A DLL file may be used in a variety of programs after it has been built. All we have to do now is add a reference to the DLL file and import it. DLL and.exe files are both executable software modules, however, DLL files cannot be directly executed.

Here we are going to make one DLL file.

Step 1: Now open your visual studio and select “Class Library (.NET Framework)”

Step 2: Now open the project and rename class name ‘class1’ to Add ‘AddNumbers’

Step 3:Now open these AddNumbers classes and add the following code

public int Add(int a, int b)
 {
   return a + b;
 }

Note: in this class, you can add all the methods which you want to use in another project with this

Now build your project and you’ll find a “calculation.dll” file in your project’s “bin/debug” directory. if you want to see your DLL file in the visual studio you need to click on ‘show all files’  because this folder is not included in our solution.

Now you can see your DLL file here :

We have completed the development of our DLL file. It will now be used in another application.

Using DLL File

Step 1: Once again open your visual studio code and create a new console app

step 2: Now open your console app and follow the following

  •  To add our DLL as a reference, right-click on the reference in the object solution and select add reference.

  • Now click on browse from the dialogue box and navigate to your DLL file and add it to the reference.

  • Now add your “using” to your Program like  using <Your ProjectName>;

Step 3: Now, add the following code to void main in your class.

AddNumbers AddNum = new AddNumbers();
int a = 0, b = 0;

Console.WriteLine("Enter the number for a :");
a = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("\nEnter the number for b :");
b = Convert.ToInt32(Console.ReadLine());

int sum = AddNum.Add(a, b);

Console.WriteLine("\n\nThe sum of a and b is" + sum);
Console.ReadKey();

That’s all there is to it; now run your console app to see what happens.

I hope you discover something beneficial that will save you some time.

Thank you very much.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories