.NET Zone is brought to you in partnership with:

Hi, This is Sourabh Sharma, Expert software developer and blogger. I write article on technology. One of my site is www.dotnetaid.com. Sourabh is a DZone MVB and is not an employee of DZone and has posted 6 posts at DZone. You can read more from them at their website. View Full User Profile

C# – Creating a DLL to Hide Your Code

  • submit to reddit

The 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  1  
Step 2: Select 'Class Library' from the list and click Ok.  2
Step 3: Now you will see the following screen 3
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. 4 
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.

Published at DZone with permission of Sourabh Sharma, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Lukasz Zwierko replied on Wed, 2013/03/06 - 5:55am

Wow another great article:

"  1. DLL is used to hide the code so anyone can use the functionality but no one can steal the code."

oh and no one will use Reflector or JustDecompile? And this guy is a Dzone MVB?? Does he even know anything about .NET????


Erik Elmore replied on Wed, 2013/03/06 - 9:42am

This is absurd.  You can't hide code by compiling it.  You can't be serious.

Erik Elmore replied on Wed, 2013/03/06 - 9:45am in response to: Lukasz Zwierko

It's not just .NET.  The simple fact is that machine-readable code is still readable code.  He may as well have posted an article about keeping secrets by writing your journal in French.

Lukasz Zwierko replied on Wed, 2013/03/06 - 10:06am in response to: Erik Elmore

Erik essentially you're right it's just that one could argue that in case of code compiled to native assembler, it takes much more time to figure out what it actually does, than in case of .NET where you can fully reverse engineer with a free tool (unless you obfuscate which was not mentioned).

But anyway, how does such a silly post find it's place on dzone, and why it's author MVB of dzone? Do you guys give it for free now? Please compare this to ericlippert.com or ayende.com. 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.