Delegate in C #

Source: Internet
Author: User

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 );
}

}
}

}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.