Dynamic Linking Library:
A
DLL is an application which can have everything what an EXE can have.
The
difference between them is, an EXE can execute independently but DLL cannot.
The
code in the DLL can be reused in many other applications but the code in EXE
cannot be reused.
Types of DLL in Windows OS:
1.
Win32 DLL: Here the code is
available in the form of simple "C" functions.
2.
COM DLL: This DLL have code in the form of reusable COM Components. These are
also referred as ActiveX DLL.
3.
.NET DLL: These DLL’s (Portable Executables) are used for distributing the
reusable classes to various types of .NET applications.
About Namespaces
1.
A namespace is a logical collection of classes and other types with unique
names. In a given namespace all the types have unique name and thus it is used
to resolve the ambiguity in case of name conflict.
2.
Any type when used outside the namespace, it must be qualified by its
namespace, but when used by another type with in the same namespace it need not
be qualified by the namespace.
3.
We can use using Namespace on top of
the file so that we don’t have to qualify all the types of that namespace in
that file.
=========================================================================================
How to Create DLL in CSharp:
Open New Project à
Select ClassLibrary à
Name as “CACreateDllExampleApp”à
Rename Class1.cs
to Math.cs
Copy below Code to
Math.cs
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
CACreateDllExampleApp
{
public class Math
{
public int sum(int a, int b)
{
return a + b;
}
public int diff(int a, int b)
{
return a - b;
}
public int product(int a, int b)
{
return a * b;
}
}
}
Build the Project.
A dll of name CACreateDllExampleApp.dll is generated in binàRelease folder.
How to Use DLL File in another Project
Create New Console
Project
è In
Solution explorer Right Click on the Project à Add Reference à
è Go
to CACreateDllExampleApp Project Folder
è In
binàReleaseà
you will find CACreateDllExampleApp.dll Add this reference to your current
project
Copy the below
code to Program.cs
Run the project.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
CACreateDllExampleApp;
namespace
CAUseDLLApp
{
class Program
{
static void Main(string[] args)
{
int x = 10;
int y = 5;
CACreateDllExampleApp.Math obj = new
CACreateDllExampleApp.Math();
string sum = obj.sum(x,
y).ToString();
string diff = obj.diff(x,
y).ToString();
string product = obj.product(x,
y).ToString();
Console.WriteLine(sum);
Console.WriteLine(diff);
Console.WriteLine(product);
Console.ReadLine();
}
}
}
No comments:
Post a Comment