Delegate is always been a weird word for the C# starters. But actually it is same as function pointers in C++.
Here I am giving one line definition and a very small easy understandable sample here.
Here I am giving one line definition and a very small easy understandable sample here.
Delegate is a reference type variable which holds the
reference for a method. The reference can be changed at run time.
Syntax:
delegate <return-type> <delegate-name>;
Ex program:
// defining Delegate
public delegate int Calculation(int x);
Public Class Test
{
int n=10; m=10;
Public int AddNum(int a)
{
int n +=a;
return n;
}
Public int MulNum(int b)
{
Int m= m*n;
Return m;
}
Public string void args[]
{
//creating instance for delegate
Calculation acalc = new Calculation(AddNum);
Calculation bcalc = new Calculation(MulNum);
//passing values to the delegate
Console.WriteLine(The Added Value is {0}",acalc(25));
Console.WriteLine(The MultipliedValue is {0}",bcalc(10));
}
}
The result will be: 35 and 100.
Yes its the way it is working. Just do an example program and get the way out....
No comments:
Post a Comment