C# – Creating a DLL to Hide Your Code
Join the DZone community and get the full member experience.
Join For FreeThe DLL also called Dynamic Link Library is a file that can be used dynamically by other programs. We create the DLL mainly to hide our confidential code so that we can use the code everywhere but can not see the code. When we talk about the layers or 3 and above tier application we need to understand one thing that why we create layers in our project. We only show some part of code to everyone and hide the main coding part. The benefit is we can use this code but can not see this.
Advantages of creating DLL:There are many advantages of using the DLL in your Application but We are talking about the main advantages:
1. DLL is used to hide the code so anyone can use the functionality but no one can steal the code.
2. Once we create a DLL then it can be used in many applications. We just have to import that dll into our project.
3. Adding a dll into your project is very simple. It reduces time to write the code again and again. In C#.Net you need to just import dll with add reference in your project.
Now we will see that how we can create a dll and then how the dll can be used in the application. Creating DLL:
Step 1: Open Visual Studio and Create New Project by Clicking New->Project
Step 2: Select 'Class Library' from the list and click Ok.
Step 3: Now you will see the following screen
Step 4: Change the name Class1. As I have changed the Name with MyCode and Write the code what you want to use in your DLL.
Step 5: Now Click Build Menu -> Build Solution to complete the process. Now you can see the dll file with .dll extention inside you project folder. Now the dll is ready to use inside other application.
Step 6: Create a New Project. Then Right click on Project Name -> Add Reference -> Select the dll File to add as reference into your project.
Step 7: Now you need to include the namespace for that dll by using the following code.
using ClassLibrary2;
Step 8: Now you can access the method of that dll and the code will be hidden to you.
You can access the method of dll file by using the following code.
private void button1_Click(object sender, EventArgs e) { int x, y; x = Convert.ToInt32(txtNo1.Text); y = Convert.ToInt32(txtNo2.Text); MyCode ob = new MyCode(); int c = ob.sum(x, y); txtResult.Text = c.ToString(); }
As in Example you can see that how we have created a dll and then how we used that dll into our project.
You can also see the articles How to use Crystal Report in C#.Net.
Opinions expressed by DZone contributors are their own.
Comments