Introduction
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace calculator
{
public class Class1
{
// Class content would go here if needed
}
}
See the file Class1.cs in the Solution Explorer.
- Now add the following methods to the Class1 to do four arithmetic operations (Addition, Substraction, Multiplication and so on).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace calculator
{
public class Class1
{
public int Sum(int n1, int n2)
{
return n1 + n2;
}
public int Sub(int n1, int n2)
{
return n1 - n2;
}
public int Mul(int n1, int n2)
{
return n1 * n2;
}
}
}
- 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.
Sample Usage of Calculator.dll in a Console Application
Use the following procedure to create a sample use of the preceding class library (Calculator.dll) in a Console Application.
- Create a new Console Application in Visual Studio.
- Add a reference to the calculator.dll as follows to access the classes and methods of the DLL in the application as follows.
- Go to Solution Explorer.
- Right-click on the References Node.
- Select the Option Add Reference.
- 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.
Click OK to add the reference to your DLL. To call the methods from the preceding Library, include the namespace, calculator.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Calculator;
Create the object of the class Class1 that contains the methods and call them as follows.
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.