How to Create Class Library (dll) in C# .NET

Creating a class library is not a hard task, it is very easy.  Class library makes our code re-usable, class library saves memory because variable in class library is declared once and used again and again for different purposes. We add some classes to our class library and it depends on the functionality of  our class library.

Extension of class library file is ".dll".
Benefits of class library 
  • Increases performance of program.
  • Saves programmer's time to write same code of function for different parameter.
  • Saves memory space.
Now we create a class library in an easy way and step by step
step 1: Firstly we have to create a new project of class library type.

Step is shown in picture below,

Step 2:
After clicking on "new project" a dialogue box is opened, we have to select the following  options listed below, visual C#->windows->class library (shown in picture below),
After selecting , we move to next step
Step 3 : Give name to our class library and solution of our project and select directory using "Browse" button (shown in picture below),
Now,our project is ready to write our source code,  so move to next step
Step 4:  In this step we have to add class in to our class library (show in picture below),
we make a "mystring"  class to public because this class is used outside, after declaration of class we have  move to next step.
Step 5: Adding some functions to our class "mystring"  according to our requirement or specification(shown in picture),
In mystring, class we have two functions,
  1. strlength
  2. concat
"strlength" funtion calculate length of string and return integer value.
"concat" funtion performing concatenation of two string and return string 
Step 6 : finally our class library is ready and built (ctrl+shift+B) shown in picture below,
Our class library is saved on our default location or directory and ready to use.
Test our class library (dll) 
Now we test our class library.
Step 1:  Again add "new project" (shown in picture below),
After creating a new project a dialog box is opened. (shown in picture below),
Again,  give name to our windows form application and solution of our project and select directory using "Browse" button,

Step 2: Add class library into our testdll windows form application (shown in picture below),
Click on “solution explorer”. Then right click on “references” and click on “add reference” a dialog box open (shown in picture below),
Now select our dll our directory  . (Shown in picture),
Click on “add” Then click on “ok” button to add our dll  (shown in picture),
Dll file is add to our current project now, 
Step 3 : use our class library  in "from1.cs" file (shown in picture below),
Step 5: Create a instance of  "mystring" class. (shown below),
"obj" is instance or object of "mystring" class and we can use function of class library using "obj",
Step 6 : Use function of "mystring" class in to our windows form application (shown below), 
Now build and run our application.

Enter string and sub string then click on check length button. (output shown below),
For concatenate string and sub string click on join buttion (output shown below),

Read more articles on C#:

Similar Articles