• How to create and Use a DLL in C#

    by  • May 14, 2014 • Microsoft .NET C# (C-Sharp), Microsoft ASP.NET • 0 Comments

    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

    • 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

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

    • Write code below in your code behind file

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



    Namecheap.com
    Avatar of GetCodeSnippet.com

    About

    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply