Delegate with a simple example.
Delegate with a simple example. Ref This link!
using System;class clsDelegate
{
public delegate int simpleDelegate (int a, int b);
public int addNumber(int a, int b)
{
return (a+b);
}
public int mulNumber(int a, int b)
{
return (a*b);
}
static void Main(string[] args)
{
clsDelegate clsDlg = new clsDelegate();
simpleDelegate addDelegate = new simpleDelegate(clsDlg.addNumber);
simpleDelegate mulDelegate = new simpleDelegate(clsDlg.mulNumber);
int addAns = addDelegate(10,12);
int mulAns = mulDelegate(10,10);
Console.WriteLine(“Result by calling the addNum method using a delegate: {0}”,addAns);
Console.WriteLine(“Result by calling the mulNum method using a delegate: {0}”,mulAns);
Console.Read();
}
}
using System;
delegate void MyDelegate(string s);
class MyClass
{
public static void Hello(string s)
{
Console.WriteLine(“ Hello, {0}!”, s);
}
public static void Goodbye(string s)
{
Console.WriteLine(“ Goodbye, {0}!”, s);
}
public static void Main()
{
MyDelegate a, b, c, d;
a = new MyDelegate(Hello);b = new MyDelegate(Goodbye);c = a + b; // call ทั้ง 2 methodd = c – a; // เหลือ call b method เดียวConsole.WriteLine(“Invoking delegate a:”); a(“A”);
Console.WriteLine(“Invoking delegate b:”); b(“B”);
Console.WriteLine(“Invoking delegate c:”); c(“C”);
Console.WriteLine(“Invoking delegate d:”); d(“D”);
}
}
//======OutPut=====//Invoking delegate a://Hello, A!//Invoking delegate b://GoodBye, B!//Invoking delegate c://Hello, C!//GoodBye, C!//Invoking delegate d://GoodBye, D!
Ref: ที่นี่ครับบทความดีๆอีกเยอะเลย TwoGuRU
September 9th, 2009 at 12:24
Good post! I plan to move into this stuff after I’m done with school, as most of it is time consuming. It’s a great post to reference back to. My blog needs more time to gain in popularity anyway.