c# delegate |
DELEGATE
“Delegate is
a point safe function pointer which is responsible to call a method and hold
the reference to execute the method”.
It is a
user type and reference type which should be declares under a namespace. To perform
delegate we have to do three steps
1. Defining a delegate
The syntax of delegate same like as method
signature but we have to add delegate word extra see this,
[<Modifier>]
delegate <return type> <Name> [(parameter list)];
Here some point to be noted,
First point is the return type of the delegate must
same to the method and the
Parameter list also must same to build delegate of a method.
Second point
is the name of the method and delegate must different for which we are going to
make a delegate.
If the
method is static you need to declare the delegate also static because by
default delegates are static.
2. Instantiating instance of the delegate
Due to the delegate is reference
type we have to instantiate the instance of delegate. This step is also similar
to the method instantiating like this
<delegate
name> db= <new> <delegate name> (method name)
db is nothing just
name like we use the object name. example will clear it.
Here also two point to be noted
First one
while instantiating you have to pass the method name for which we build the
delegate instead of giving parameter values.
Second
point is if the method is static it is directly call in method name if the main
and delegate are in same class otherwise you need to give the class name as we
did in the static method to execute, if it is non-static you should have to go
with the reference or instance to call the method name in delegate method name.
3. Call the delegate
Where we call the delegate by passing the required
values, but internally actually the method that is bound with the delegate get
execute and give us the results. Like this
<Delegate name ‘db’>
(parameter); //db as we use for
delegate instance.
c# delegate examples |
EXAMPLE OF DELEGATE IN C#
Here I give two examples of a simple program
that have three method addition ( ) subtraction ( ) and Hello ( ). In first
example no delegate will be used and in second for the execution we will use
delegate let’s get start.
Example 1(normal method without using delegate)
Method class
namespace TestDelegate
{
class TestMethod
{
public void Add(int a,int b)
{
Console.WriteLine(a + b);
}
public int Sub(int a,int b)
{
return a - b;
}
public static string Hello(string
str)
{
return "Hello " + str ;
}
}
}
|
Main class
namespace TestDelegate
{
class Program
{
static void Main(string[] args)
{
TestMethod t = new TestMethod();
t.Add(12, 13);
Console.WriteLine(t.Sub(22,12)) ;
Console.WriteLine(TestMethod.Hello("Ahmed"));
Console.ReadKey();
}
}
|
Example 2(by using delegate)
Class method
{
public delegate void Addnums(int x, int
y);
public delegate int Subnums(int a, int
b);
public delegate string SayHello(string
str);
class TestMethod
{
public void Add(int a,int b)
{
Console.WriteLine(a + b);
}
public int Sub(int a,int b)
{
return a - b;
}
public static string Hello(string
str)
{
return "Hello " + str ;
}
}
}
|
Main class
namespace TestDelegate
{
class Program
{
static void Main(string[] args)
{
TestMethod t = new TestMethod();
Addnums ad = new Addnums(t.Add);
Subnums sb = new Subnums(t.Sub);
SayHello sy = new
SayHello(TestMethod.Hello);
int sub = sb(22, 12);
ad(12, 13);
Console.WriteLine(sub) ;
Console.WriteLine(sy("Ahmed"));
Console.ReadKey();
}
}
}
|
0 comments:
Post a Comment
Please do not enter any spam link in the comment box