C # Delegate essence exploration I. Initial test Delegation

Source: Internet
Author: User

// When I first listen to the delegate, I understand it as: one person entrusts another person to do something.
// According to this idea, the following test program is designed
// Leaders entrust their subordinates to do something.

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Reflection;

Namespace ConsoleApplication1
{
Public class FirstTest
{
Public static void test ()
{
Lead Zhu Ge = new lead ();

Zhu Ge. Issue the task ();
}
}

Delegate string delegate task (string task content );

Class leader
{
Subordinate Wei = new subordinate ();

Public void issue task ()
{
String execution result;

A delegated task;
One delegated task = new commissioned task (Wei. His job );
// A delegated task = Wei. Current job;

Execution result = a commissioned task ("Operation World of Warcraft ");
Console. WriteLine (execution result );
Execution result = a delegated task ("processing industry competition ");
Console. WriteLine (execution result );

// This is not more direct
Execution result = Wei. His job ("operating World of Warcraft ");
Console. WriteLine (execution result );
Execution result = Wei. His job ("handling industry competition ");
Console. WriteLine (execution result );

// Use Type. InvokeMember
Object subordinate = typeof (subordinate). InvokeMember (null, BindingFlags. CreateInstance, null );
Execution result = (string) typeof (subordinate). InvokeMember ("current job", BindingFlags. InvokeMethod, null, a subordinate, new object [] {"World of Warcraft operations "});
Console. WriteLine (execution result );
}

}

Class subordinates
{
Public string current job (string task content)
{
StringBuilder work content = new StringBuilder ();
If (Task content = "Operation World of Warcraft ")
{
Work content. AppendLine ("arrange device procurement ");
Work content. AppendLine ("Recruitment customer service, induction training ");
Work content. AppendLine ("advertising ");
Work content. AppendLine ("game market ");
Work content. AppendLine ("launch activity ");
Work content. AppendLine ("............ ");
}
Else if (Task content = "processing industry competition ")
{
Work content. AppendLine ("investigating competitors ");
Work content. AppendLine ("fight ");
}
Return work content. ToString ();
}

}
// From this example, we can see that there are many ways to entrust others to do something, and entrusting technology is the most bullshit.
// Does this indicate that my understanding of delegated technology has entered a misunderstanding. Delegated technology is not used in this way at all.
// From the programming perspective, delegate technology is definitely not a technology born to implement a class to let another class execute a method.
// To implement such a function, you only need to instantiate another class and call the instance method to achieve the purpose.

// What is the essence of delegated technology?
// Observe the Definition Statement delegate string delegate task (string task content );
// In terms of level, delegate and class are of the same level.
// If the subsequent string delegate task (string task content) is regarded as an object entity
// Can delegate be understood as an object definition character such as class?
// This operator is used to define a method.

// Continue to observe the instantiation process of the "delegate task"
// One delegate task = new delegate task (Wei. His job );
// Careful analysis:
// First, the "subordinate" class has been instantiated before "A delegate task" is instantiated.
// "Wei. His job" is an address pointer pointing to the "subordinate" class instance "Wei" method "his job ".
// "Wei. His job" pointer follows the structure of the "delegate task" class, instantiate it as a new method pointer, and assign it to the "one delegate task" variable.
// This assignment process can be changed to the following: a delegated task = Wei. His job;
// Compare the instantiation process of the class. MyClass aClass = new MyClass (Type instance );
// The constructor of the delegate type needs to input the Method Instance parameter. The parameter type is the type defined by delegate.

// Look back at the delegate string delegate task (string task content) in the type definition section );
// There is only one line of declaration, no content, which is very abstract. That's right. This is not a definition of an abstract class without content!

// To sum up, the essence of delegate is to define the type of an abstract method. to instantiate an abstract method, you need to input an instance pointer.
// The Word "delegate" is far away from me. I have semantic dependencies and I am dizzy when I see the word "delegate! I cannot understand the word delegate in. NET.

// Why is there a difference between delegate definition and instantiation performance and class? No matter whether it is defined or instantiated, It is a deeper issue. In short, this is a usage.
// * The string is actually an escape of System. String. The delegate is the same as the using delegate = System. Delegate;
// * System. Delegate will not be studied first. It is necessary to study its usage.

}

// Test code

FirstTest. Test ();

Related Article

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.