Understanding about Delegation
Code
Using System;
Namespace guoxinghua
{
Public delegate int MathOptDelegate (int value1, int value2 );
Class Class
{
Public int Add (int argument1, int argument2)
{
Return argument1 + argument2;
}
}
Class Program
{
Static void Main (String [] args)
{
MathOptDelegate oppDel;
MathOpt obj = new MathOpt ();
OppDel = obj. Add; // assign a value to this delegate type
Console. WriteLine (oppDel (1, 2); // 3
}
}
}
Code
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace guoxinghua
{
Public delegate int MathOptDelegate (int value1, int value2 );
Class Class
{
Public static void Main (String [] args)
{
MathOpt myopt = new MathOpt ();
Myopt. Add (1, 2 );
MathOpt oppDel;
OppDel = myopt. Add;
OppDel (1, 2 );
Console. WriteLine (PrintResult (MathOpt. Add, 1, 2 ));
}
Public static int PrintResult (MathOptDelegate action, int a, int B)
{
Int result = action (1, 2 );
Return result ;}
}
Public class MathOpt
{
Public static int Add (int arg1, int arg2 ){
Return arg1 + arg2;
}
Public static int Sub (int arg1, int arg2 ){
Return arg1-arg2;
}
}
}
Code
Using System;
Namespace guoxinghua
{
Delegate double DoubleOp (double x );
Class MathOperations
{
Public static double multi (double value ){
Return value * 2;
}
Public static double Square (double value)
{
Return value * value;
}
}
Class MainEntryPoint
{
Public static void Main (String [] args)
{
DoubleOp [] operations = {
New DoubleOp (MathOperations. MultiByTwo), new DoubleOp (MathOperations. Square)
}
For (int I = 0; I <operation. length; I ++)
{
Console. WriteLine ("Using operations [{0}]", I );
ProcessAndDisplayNumber (operation [I], 2.0 );
Console. WriteLine ();
}
}
Public static void ProcessAndDisplayNumber (DoubleOp action, double value)
{
Double result = action (value );
Console. WriteLine ("Value is {0}, result of operation is {1}", value, result );
}
}
}
Code
Using System;
Namespace guoxinghua
{
Public delegate void Del (String testString)
Class MulticastDelegate
{
Public static void Main (String [] args)
{
MessageTest OK = new MessageTest ();
Del d1 = new Del (OK. Message1 );
Del d2 = new Del (OK. Message2 );
Del d3 = new Del (OK. Message3 );
Del dAll = d1 + d2 + d3;
DAll ("proxy broadcast ");
DAll = aAll-d1;
DAll ("proxy-Based Multi-Channel delegation ");
}
}
Class MessageTest
{
Public void Message1 (String testString ){
Console. WriteLine ("using message1" + testString );
}
Public void Message2 (String testString)
{
Console. WriteLine ("using message2" + testString );
}
Public void Message3 (String testString)
{
Console. WriteLine ("using message3" + testString );
}
}
}
C # anonymous method of 2.0
Code
Using System;
Namespace guoxinghua
{
Class Class
{
Public delegate void MyDelegate (int x );
Public static void Main (String [] args)
{
MyDelegate md;
Md = SomeMethod;
Md (100 );
Console. WriteLine ("guoxinghua ");
}
Static void SomeMethod (int x)
{
Console. WriteLine (x );
}
}
}
Code
Using System;
Namespace guoxinghua
{
Public delegate void MyDelegate (int x );
Class Class
{
Public static void Main (String [] args)
{
MyDelegate md = delegate (int x ){
Console. WriteLine (x );
}
}
}
}