Previously, we learn about CSS in CSS-3 And CSS-4. now we see how to create DLL's in C#
In this article, I’m explaining how to create and use dl in C#.
1. Create DLL:
Open visual studio >> Select File >> New >> Project >> Windows >> Class Library.
Select your project name and appropriate directory using the browse button and click
OK.
Project and its file:
In this project add two classes by default Class1.cs and AssemblyInfo.cs inside properties folder. We will be concentrating only Class1.cs
Calculator Namespace:
When you open Class1.cs then you see Calculator Namespace. We will be
referencing this Namespace in our clients to use this class method.
using System;
namespace Calculator
{
public class Class1
{
}
}
Now add some calculator method in your project.
using System;
namespace Calculator
{
public class Class1
{
public static int Add(int num1,int num2)
{
return num1 + num2;
}
public static int Sub(int num1, int num2)
{
return num1 - num2;
}
public static int Mul(int num1, int num2)
{
return num1 * num2;
}
public static int Division(int num1, int num2)
{
return num1 / num2;
}
}
}
Now Build your project after build succeeded generate Calculator.dll file inside bin >> Debug folder.
Use DLL:
Make a client application for using the DLL file.
Create a console application:
Open visual studio >> Select File >> New >> Project >> Windows >> Console Application.
Select your project name and appropriate directory using the browse button and click OK.
Add reference of the Namespace:
Now add a reference to the library. Project >> Add reference
Now on this page click browse button to browse your library.
Use Calculator Namespace and call method.
using Calculator;
namespace UseDll
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Your Operator Choice Add + Sub - Mul * Division /");
char choice = Convert.ToChar(Console.Read());
switch (choice)
{
case '+':
{
int add = Class1.Add(10, 20);
Console.WriteLine("Sum of the two number " + add);
break;
}
case '-':
{
int sub = Class1.Sub(50, 20);
Console.WriteLine("Substrack of two integer number " + sub);
break;
}
case '*':
{
int mul = Class1.Mul(2, 4);
Console.WriteLine(mul);
break;
}
case '/':
{
int div = Class1.Division(10, 2);
Console.WriteLine(div);
break;
}
default:
{
Console.WriteLine("Wrong input!!");
break;
}
}
}
}
}
Output
You may also want to read: Creating C# Class Library (DLL) Using Visual Studio .NET
In my next post, we are going to learn about : Join, Sleep and Interrupt methods in C# Threading
Leave Comment