Class Library Creation in .NET

This article explains how to create a Class Library and how to use a Class Library (Calculator.dll) in a Console Application.

  • We can create a class library (in other words DLL file ) of code containing classes and methods.
  • One DLL file can be used in many applications in .Net like Console Application, Windows Application and Web Application and so on.
  • To use a DLL file in an application we need to add a reference to the DLL file.
  • DLL is not a self-executable file instead it is to be shared by many applications for code reusability purposes.

Procedure to Create a Class Library

  • Start a new project in Visual Studio from the New Project dialogue box, select Class Library Template from the Project Template Window.
  • In the Name box, type a name for your Class Library, say Calculator. Then click OK.
  • When you click OK, it will create a namespace with the name we gave for the Library and you will see the following in the code window:
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5.   
    6. namespace calculator  
    7. {  
    8.     public class Class1  
    9.     {   
    10.     }  

    See the file Class1.cs in the Solution Explorer.

    Solution Explorer
  • Now add the following methods to the Class1 to do four arithmetic operations (Addition, Substraction, Multiplication and so on).
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5.   
    6. namespace calculator  
    7. {  
    8.     public class Class1  
    9.     {  
    10.     public int sum(int n1, int n2)  
    11.         {  
    12.             return (n1 + n2);  
    13.         }  
    14.         public int sub(int n1, int n2)  
    15.         {  
    16.             return (n1 + n2);  
    17.         }  
    18.         public int mul(int n1, int n2)  
    19.         {  
    20.             return (n1 + n2);  
    21.         }  
    22.     }  
  • In the preceding code, we've added three simple methods, one for addition, the second for substraction and the third one for multiplication. Similarly we can add more methods to class1 for other operations.
  • Now press F5 from the keyboard to start debugging. If everything is okay then a DLL will be created with the name Calculator.dll in the debug folder under the bin folder.

Use the following procedure to create a sample use of the preceding class library (Calculator.dll) in a Console Application.

  1. Create a new Console Application in Visual Studio.
  2. Add a reference to the calculator.dll as follows to access the classes and methods of the DLL in the application as follows:
  3. Go to Solution Explorer
  4. Right-click on the References Node
  5. Select the Option Add Reference
  6. It will display a dialog box with a few tabs on it. Click the Browse tab. Browse to where you saved your Class Library. Then locate the Bin > Debug folder from above. Your DLL will be in there:

    DLL

    Click OK to add the reference to your DLL.

    To call the methods from the preceding Library, include the namespace, calculator.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using Calculator;  
    Create the object of the class Class1 that contains the methods and call them as follows:

    Object of the class
Run your programme. This is an easy way to create your own class libraries and reuse it in any .Net application and in any .Net Language.