top button
Flag Notify
Site Registration

How to create and Use a DLL in C#?

+1 vote
299 views
How to create and Use a DLL in C#?
posted Feb 2, 2016 by Sathyasree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

A Dynamic Link Library (DLL) is a file that can be used by other programs dynamically. It is a library that contains code, functions and data that can be used in another program or more than one program. So once we have created a DLL file, we can use it in many applications. The major advantage of DLL is that it doesn’t get loaded into memory with the program. It will load into memory when we use it or when we call it in our application. So DLLs are usually loaded on demand. DLL is also used to hide the code so that anyone can use only the functionality but not the code.

To use a DLL or code of the DLL in our application, only thing we need to do is to add the reference of that DLL in our application. When we have some reusable code and we need to use it in more than one application, we can create a DLL and put our code in that DLL. Then we can add a reference of that DLL in our application to use functions of that DLL. Both DLL and exe files are executable programs but the difference is that a DLL cannot be executed directly or independently.

There are few steps to create a DLL, we will see these steps in this article and also we will see how we can use this DLL in our application.

  1. Open Visual Studio and create a new Class Library Project in C#
  2. Rename the project to CreateDLL and rename the class to EmployeeClass.cs
  3. Now write below code in Employee Class

    // First method to get employee name
    public string GetEmployeeName(int id)
    {
    string name = "";
    switch (id)
    {
    case 1:
    name = "Richard Wand";
    break;
    case 2:
    name = "Susan Jacob";
    break;
    default:
    name = "";
    break;
    }
    return name;
    }
    // second method to get Salary
    public int GetSalary(int id)
    {
    int salary = 0;
    switch (id)
    {
    case 1:
    salary = 27500;
    break;
    case 2:
    salary = 35700;
    break;
    default:
    salary = 0;
    break;
    }
    return salary;
    }

  4. Now build solution to create CreateDLL .dll file. You can see this file in debug directory under bin.

Now we will see how we can use this DLL in our application

  1. Create a new website in Visual Studio
  2. Go to Solution Explorer and right click on project and click on Add Reference
  3. Go to Browse tab and find the CreateDLL.dll file and click Ok to add it in your website.
  4. Add a web form and write below controls in Default.aspx file.

asp:Label>



asp:Label>

  1. Don’t forget to add following namespace in your Default.aspx.cs file

    using CreateDLL;

  2. Now write below code in your file

    protected void Page_Load(object sender, EventArgs e)
    {
    EmployeeClass ObjEmp = new EmployeeClass();
    string name = ObjEmp.GetEmployeeName(1);
    lblName.Text = "Welcome " + name;
    }
    protected void btnGetSalary_Click(object sender, EventArgs e)
    {
    EmployeeClass ObjEmp = new EmployeeClass();
    int salary = ObjEmp.GetSalary(1);
    lblSalary.Text = "Your Salary is: $" + salary.ToString();
    }

  3. Debug the code and see it in your browser.

answer Feb 2, 2016 by Shivaranjini
Similar Questions
+1 vote

This example shows you how to check given input contains only numbers (digits) or not using regular expression in asp.net and c#.

Regular expression
string strRegexpNumber = @"^[0-9 ]*$";

...