Creating and Using DLL (Class Library) in C#
A class library file is a collection of classes and namespaces in C# without any entry point method like Main. Once we create a class library file it can be used in the C# project and classes inside it can be used as required. Class Library makes it convenient to use functionalities by importing DLL inside the program rather than redefining everything. So, Let’s make our own class library in C#.
Class Library: It is a package or file that contains different namespaces and class definitions that are used by other programs.
Steps to Create and Use DLL in Visual Studio
Step 1: Create a New Project in Visual Studio. Once You open visual studio it should open Create New Project OR You can click on the file and select the new project option. Select C# as language and then Select Class Library Option. Click Next.
Step 2: On the next screen configure your class library project name. Make sure you give a different name for the Solution. Then click next.
Step 3: On the next screen select the .NET version. I have selected 7.0. Then click create which will create a project.
Step 4: Once the Project is created a C# file will already be created with namespace as project name and class Class1. Let’s add some code inside the class that will print something when the method sayHello() inside the class is called.
Step 5: After writing the code click on the build in the menu bar and click build geeksforgeeks.
Step 6: If everything is correct you should get build success in the output below the editor.
Step 7: Now the DLL file is created inside the project-folder/bin/Debug/net7.0 folder which can be used.
Step 8: Let’s create a new project to use this DLL file. For the new project select Console App from the list and click next.
Step 9: Configure a new project with a name and give the same name for the solution as given for creating a Class library project OR just select the same folder for a solution.
Step 10: Once the project is created it should have a program.cs file opened. Type the Code inside the program.cs file. import the DLL file inside the program by putting “using geeksforgeeks” at the top. Now we can use Class1 inside our program. Call the sayHello() method by creating an object of Class1.
Step 11: Afterwards we have to add a reference of the DLL file to our project. For that in Solution Explorer select dependencies under our project name and right-click to select “Add Project reference”.
Step 12: Now select our Class Library project name from the list. Click Ok. Now we can build our project.
Step 13: From the menu click build and select build “geeksforgeeksapp” or your console project name. You should see the build succeeded at the output.
Step 14: Once Build is succeeded right click on Class Library Project from Solution Explorer and select unload the project. Then click on run without debugging on the sub-menu bar at the top (play button).
Step 15: You should see a console window opened with say hello message printed.
Steps to Create DLL file with C# Compiler
Step 1: Create a new blank file inside your favorite editor and save it as a “.cs” file with the name you want for DLL. Add the Code to the file with namespace and class with any method. I have added the same method as above which prints “Hello From GeeksForGeeks”.
Step 2: Open Command Prompt or Terminal where the CS file is saved. Compile the program with the CSC compiler and make sure you add the target file as a library which will generate a DLL file. If there is no error, then you should see a DLL file created inside a folder with the name as the filename.
Step 3: Now let’s use this DLL. Create another file inside the same folder where DLL is located and save it as “.cs”. Type your code to use the DLL. I have written the same code as above.
Step 4: Then save the file and compile it with CSC as follows. We have used /r to provide references for our DLL file.
Step 5: If no error compilation is successful and we can run our program type filename.exe to run the program. You should see the output printed.
Please Login to comment...