Dynamic Link Library


Dynamic Link Library, commonly known as DLL, are shared libraries used on Microsoft platforms (read by: Windows operating system). Think of it as a supporting file that contains useful information (data or functions) that can be used by an application (or other DLL file) during run-time.

Why use DLL Files rather than CSPROJ Files Used?

There are three reasons for using dynamic link library files over CSPROJ or SLN files:
  1. Our application file is smaller since we can hold data/code in a separate file.
  2. Because the DLL file is separate from the running application, the RAM that would normally be required by the DLL file isn't used until the applications load the DLL file.
  3. Since our reusable code is held in a DLL file, if we use that code in many projects, but find a mistake in the DLL code and need to change it, we can simply recompile the DLL file and replace the old DLL file from all of our projects, as opposed to needing to edit raw code in all our projects and recompiling all of those projects.

How to create our own DLL File?

So now that we know what a DLL file is and why it's used, let's go through an example of making one. Our example is going to be in C#. We're going to create a Class Library (.NET Standard) project in Visual Studio, and add some functions to a class for the library. In our example, we're going to create a Math library that happens to have an Operator class. This Operators class has three functions, one for adding, one for multiplying, and one for subtraction. When we build our project, a .dll file is generated in the bin folder of the project. And that's it! That's our DLL file that contains the code for our shared library.
Dynamic Link Library  
Now that we've created a DLL file, let's actually use it in a different project. Let's go ahead and create a basic C# Console App in Visual Studio.
Here are the steps to use the Math DLL:
  1. Right-click on the console project name and add a Reference to the DLL file we created, 'Math'. (Browse for it on your computer).
  2. To the main file in our current project, use the namespace that we used in our DLL file, 'Math'.​
  3. Inside of our Main function, instantiate the class that we want from that namespace, 'Operators', and use the functions for that class, 'Addition'.
And just like that, we've used the DLL file that we created.