Main menu

A Dynamic Link Library, also called DLL, is a file that isdynamicallyused by other programs.DLL is a library file that contains code, functions and data that can be used in another program or more than one program.One DLL file can be used in many other applications by adding reference of this DLL. A DLL doesn’t get loaded into memory with the program instead it will load into memory when we need it or when we use it in our application. This is one of the major advantages of DLLs thatthese areloadedinto our application on demand. Another advantage ofDLL is that it can be used to hide the code so that only the functionality of that DLL can be used but not the code. Another advantage iswhen 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 same in the sense that they are executable programs but the difference is that a DLL cannot be executed directly or independently while on the other hand an exe file can be executed independently.

We can easily create a DLL in C# and there are only few steps to do it. I will explain these steps in this article. Use a DLL or code of the DLL in our application is also very easy. We only need to add the reference of that DLL in our application which we DLL want to use.Below, I will show you the steps of creating a DLL file and later I will show you how easily we can use this DLL in our application.

Open Visual Studio and create a new Class Library Project in C#

Rename the project to CreateDLL and rename the class to Customer.cs

Now write below code in Customer.cs file

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

publicstringGetCustomerName(intcustId)

{

stringcustName="";

if(custId==1)

{

custName="Ashley Giles";

returncustName;

}

elseif(custId==2)

{

custName="Martin Green";

returncustName;

}

else

{

returncustName;

}

}

publicstringGetCustomerCountry(intcustId)

{

stringcustCountry="";

if(custId==1)

{

custCountry="England";

returncustCountry;

}

elseif(custId==2)

{

custCountry="USA";

returncustCountry;

}

else

{

returncustCountry;

}

}

Now build solution. You can see a file created in debug directory under bin directory with the name of CreateDLL .dll.

Now we will see how we can use this DLL in our website.

Create a new website in Visual Studio and name it UseDLL

From menu, click website and click on Add Reference

Go to Browse tab and find the CreateDLL.dll file and click Ok to add it in your website.

Add a web form and write below controls in Default.aspx file

1

2

3

4

<asp:Label ID="lblCustName"runat="server"></asp:Label><br/>

<asp:Button ID="btnShowCountry"runat="server"Text="Show Country"

onclick="btnShowCountry_Click"/><br/>

<asp:Label ID="lblCustCountry"runat="server"></asp:Label><br/>

Open code behind file and add following namespace. This is also important to use DLL we have created.

1

using CreateDLL;

Write code below in your code behind file

1

2

3

4

5

6

7

8

9

10

11

12

protectedvoidPage_Load(objectsender,EventArgse)

{

Customer custObj=newCustomer();

stringcustName=custObj.GetCustomerName(2);

lblCustName.Text="Customer Name: "+custName;

}

protectedvoidbtnShowCountry_Click(objectsender,EventArgse)

{

Customer custObj=newCustomer();

stringcustCountry=custObj.GetCustomerCountry(2);

lblCustCountry.Text="Customer Country: "+custCountry;

}

Now browse your website in browser and see the result. You can download complete code sample from below link.