C # delegate essential exploration II. Test Method Variables

Source: Internet
Author: User

// Delegate a type definition keyword to define an abstract method.
// Instance pointer needs to be passed in the Process of instantiating this type.
// The result of Instantiation is to get a method pointer.

// The abstract method is used to define the Method Instance as a variable.
// Perform several tests based on this point of view.
// Test 1. Use the abstract method as a common variable

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

Namespace ConsoleApplication1
{
Delegate string abstract method (string parameter );

Class SecondTest
{
Public abstract method variable;

Public SecondTest ()
{
// If you want to use this method variable, you must first instantiate it, And then instantiate the method variable during class instantiation.
Method variable = new abstract method (this. Method entity );
}

// Define an object here. The instance must be passed in following the delegate type instantiation process. This code must exist.
Public string method entity (string method parameter)
{
StringBuilder work content = new StringBuilder ();
If (method parameter = "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 (method parameter = "processing industry competition ")
{
Work content. AppendLine ("investigating competitors ");
Work content. AppendLine ("fight ");
}
Return work content. ToString ();
}

}

// In the preceding example, both "method variable" and "method entity" are published as public so that they can be seen in test2.
// The method object is called externally. This is easy to understand. The object method is called.
// Method variable published ?!! We use a variable to record changes not just to define one read later, but to store values.
// When it comes to changes, what is the effect of the changes in the method variable value?
// It should be the method variable pointer pointing to a change, that is, pointing to another method.
// Let's use another example to demonstrate this change.
}

// Call Method

String execution result;
SecondTest test2 = new SecondTest ();
// Directly call the method entity
Execution result = test2. method entity ("Operation World of Warcraft ");
Console. WriteLine (execution result );
// Call method Variables
Execution result = test2. method variable ("processing industry competition ");
Console. WriteLine (execution result );

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.