Basic delegated knowledge

Source: Internet
Author: User

A delegate is a data structure that references static methods or reference class instances and Their instance methods (msdn );

The Declaration of the delegate type establishes an agreement that points to the signature of one or more methods;

Delegation is object-oriented and type-safe.

1. A simple example of delegate:

The delegate Declaration requires the keyword delegate;

The delegate name is arbitrary, but the delegate must match the method signature it points to: return type; number of parameters and type

Namespace consoleapplication1
{

// Define the delegate, which can point to any of the following methods: the return value is int type, and there are two int Parameters
Public Delegate int mathdelegate (int x, int y );
Public classdelegatedemo
{
Public static int add (int x, int y)
{
Returnx + Y;
}
Public static int multiply (int x, int y)
{
Returnx * Y;
}
Public static int subtract (int x, int y)
{
Returnx-y;
}
}
Public classprogram
{

Static voidmain (string [] ARGs)
{
// Create a delegate instance pointing to the delegatedemo. Add () method
Mathdelegate math1 = newmathdelegate (delegatedemo. Add );
// Call add () to use the delegate
Console. writeline ("100 + 20 = {0}", math1 (100, 20 ));
// Create a delegate instance pointing to the delegatedemo. Multiply () method
Mathdelegate math2 = newmathdelegate (delegatedemo. Multiply );
Console. writeline ("100*20 = {0}", math2 (100, 20 ));

Console. Readline ();
}

}
}

Ii. Delegated Multicasting)

Multicast means that a delegate object can point to multiple methods instead of just pointing to one method;

+ = Add method,-= Remove Method

The Return Value of the method pointed to by multicast delegation is generally void. Of course, if there is a return value, only the return value of the last method can be obtained.

 

Namespace consoleapplication1
{

// Define the delegate, which can point to any of the following methods: the return value is int type, and there are two int Parameters
Public Delegate int mathdelegate (int x, int y );
Public class delegatedemo
{
Public static int add (int x, int y)
{
Console. writeline ("addition Result :");
Console. writeline ("{0} + {1} = {2} \ n", X, Y, x + y );

Return X + Y;
}
Public static int multiply (int x, int y)
{
Console. writeline ("Multiplication Result :");

Console. writeline ("{0} * {1} = {2} \ n", X, Y, x * y );

Return x * Y;
}
Public static int subtract (int x, int y)
{
Console. writeline ("subtraction Result :");

Console. writeline ("{0}-{1} = {2} \ n", X, Y, x-y );

Return x-y;
}
}
Public class Program
{

Static void main (string [] ARGs)
{
// Create a delegate instance pointing to the delegatedemo. Add () method
Mathdelegate math = new mathdelegate (delegatedemo. Add );
// Use the same instance created above to call the delegatedemo. Multiply () method and the delegatedemo. Subtract () method
Math + = new mathdelegate (delegatedemo. Multiply );
Math + = new mathdelegate (delegatedemo. Subtract );

Int result = math (100, 20 );
Console. writeline ("delegate call result: {0}", result );

Console. Readline ();
}

}
}
Iii. Delegated change and Inverter

When the method signature matches the delegate type, the covariant and inverter can provide a certain degree of flexibility;

The covariant allows a method to have more derived return types than those defined in the delegate;

The inverse method allows fewer derived parameter types than the delegate type. (msdn)

Namespace consoleapplication1
{
Public Delegate person persondelegate ();
Public class delegatecovariance
{
Public static person getperson ()
{
Return new person ();
}

Public static student getstudent ()
{
Return new student ();
}
Static void main (string [] ARGs)
{
Persondelegate = new persondelegate (getperson );

Person = persondelegate ();
Console. writeline ("Person: {0}", person. tostring ());

Persondelegate studentdelegate = new persondelegate (getstudent );

Student = (student) studentdelegate (); // if there is no strong type conversion, you cannot compile console. writeline ("Student: {0}", student. tostring ());

Console. Readline ();
}
}

Public class person
{
Protected string _ firstname;
Protected string _ lastname;
Protected int _ age;

Public Person ()
{
This. _ firstname = "Smith ";
This. _ lastname = "Jack ";
This. _ age = 20;
}
Public override string tostring ()
{
Return string. Format ("firstname: {0} \ tlastname: {1} \ Tage: {2}", _ firstname, _ lastname, _ age );
}
}
Public class student: person
{
Protected string _ height;
Public student ()
: Base ()
{
_ Height = "175 ";
}
Public override string tostring ()
{
Return string. format ("firstname: {0} \ tlastname: {1} \ Tage: {2} \ theight: {3}", _ firstname, _ lastname, _ age, _ height );
}
}
}
By writing basic simple examples, you can master the basic knowledge of delegation. The next section mainly describes how to learn the asynchronous call of delegation.
End

 


 

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.