C # delegate essential exploration 3. Call method Variables

Source: Internet
Author: User

// Test 2. Change the variable value of the Method
// Here the delegate of test 2 is used without recreating a new one.
// Delegate string abstract method (string parameter );

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

Namespace ConsoleApplication1
{
Class ThirdTest
{
Public void DoTest ()
{
Original object instance = new original object ();

String execution result;
// Test direct method call
Execution result = object instance. original method entity ("Operation World of Warcraft ");
Console. WriteLine (execution result );

// Call the test method variable
Execution result = object instance. Method variable ("processing industry competition ");
Console. WriteLine (execution result );

// Change the method variable value
Object instance. Method variable = new abstract method (this. new Method entity );
Execution result = object instance. Method variable ("Work handover ");
Console. WriteLine (execution result );
}

New Method entity of private string (string method parameter)
{
StringBuilder work content = new StringBuilder ();
If (method parameter = "Work handover ")
{
Work content. AppendLine ("submit materials ");
Work content. AppendLine ("package and quit ");
}
Return work content. ToString ();
}

}

Class Original object
{
Public abstract method variable;

Public original object ()
{
// Instantiate method Variables
Method variable = new abstract method (this. original method entity );
}

// Define the original object
Public string original 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 ");
}
Else
{
Work content. AppendLine ("input parameter exception ");
}
Return work content. ToString ();
}

}

// Here we have seen some effects of changing the value of the method variable.
// Publish a method variable to the outside. When the variable value changes, the next call points to the changed method call.
// Variable operators include =, + =,-=, and so on.
// If the method variable is used, the redirection, multiple directions, and redirection of the method address pointer are removed.
// The method variables have been noticed and are not used in the original object itself.
// The example below adds the internal usage of the object.
}

// Test code

ThirdTest test3 = new ThirdTest ();
Test3.DoTest ();

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.