C # delegate essential exploration 4. Mixed calling of method Variables

Source: Internet
Author: User

// Delegate defines only rules and has no entity at all. It should be called a method interface.
// Test 3. Add an internal call to the method variable

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

Namespace ConsoleApplication1.FourthTest
{
Delegate void method interface (string parameter );

Class FourthTest
{
Public void DoTest1 ()
{
Basic Object object instance = new basic object ();

Console. WriteLine ("Test 1 :");
// Call the test method variable
Console. WriteLine ();
Console. Write ("method variable call :");
Object instance. Transaction Processing ("initial transaction ");

// Append the method variable value
Console. WriteLine ();
Console. Write ("APPEND method variable value :");
Object instance. Method variable + = new method interface (this. new Method entity );
Object instance. Method variable ("APPEND transaction ");
Object instance. view the original entity ();

// Remove Method variable values
Console. WriteLine ();
Console. Write ("Remove Method variable value :");
Object instance. Method variable-= new method interface (this. new Method entity );
Object instance. Method variable ("execute a transaction after the append transaction is removed ");

// Change the method variable
Console. WriteLine ();
Console. Write ("change method variable :");
Object instance. Method variable = new method interface (this. new Method entity );
Object instance. Method variable ("Redirect transaction processing ");
Object instance. view the original entity ();
}

Public void DoTest2 ()
{
Basic Object object instance = new basic object ();

Console. WriteLine ("Test 2 :");
// Call the test method variable
Console. WriteLine ();
Console. Write ("method variable call :");
Object instance. Transaction Processing ("initial transaction ");

// Append the method variable value
Console. WriteLine ();
Console. Write ("APPEND method variable value :");
Object instance. Method variable + = this. New Method entity;
Object instance. Method variable ("APPEND transaction ");
Object instance. view the original entity ();

// Remove Method variable values
Console. WriteLine ();
Console. Write ("Remove Method variable value :");
Object instance. Method variable-= this. New Method entity;
Object instance. Method variable ("execute a transaction after the append transaction is removed ");

// Change the method variable
Console. WriteLine ();
Console. Write ("change method variable :");
Object instance. Method variable-= this. New Method entity;
Object instance. Method variable ("Redirect transaction processing ");
Object instance. view the original entity ();
}

New Method entity of private void (string method parameter)
{
Console. WriteLine (method parameter. ToString ());
}
}

Basic class Object
{
Public method interface method variable;

Public Basic Object ()
{
// Instantiate method Variables
Method variable = new method interface (this. original method entity );
// Method variable = this. original method entity;
}

// Define the original method entity
Private void Original method entity (string method parameter)
{
Console. WriteLine ("original method entity:" + method parameter. ToString ());
}

Public void transaction processing (string transaction)
{
Method variables (transactions );
}

Public void view original entity ()
{
This. original method entity ("View Original entity situation ");
MySize a = new MySize (5, 5 );
MySize B =;
}
}

Class MySize
{
Int _ width;
Int _ height;
Public MySize (int width, int height)
{
_ Width = width;
_ Height = height;
}
}
// During this test, the execution of the "method variable" called in the "transaction processing" of the basic object is affected.
// Use + = to retain the original method called for "method variable" in "transaction processing.
// Use = to clear the original value of "method variable" and redirect it to the new method.
// We have implemented the transaction processing process for changing the "Basic Object" from the external.
// Comment out the method variable = new method interface (this. original method entity) in the constructor of "Basic Object );
// Replace it with the method variable = this. original method entity;
// Observe the execution result of "view the original object situation" and find that there is no change, which means that the original method object pointer will not be changed when the value of the method variable is changed.
// Int a = 5;
// Int B =;
// B = 3; then a is 5.
// Write Int B = new Int (a) in this way. It is quite fancy and useless at present.

// The above example is the event principle. Take the button click event as an example:
// Define a global method interface MouseEventHandler.
// The Button class internally defines a public method variable Click, whose type is MouseEventHandler.
// Click is called when the WM_LBUTTONUP is captured cyclically in the WndProc of the Button class.
// The Button class defines an OnClick object to facilitate Click and implement the interface painting function.
// The method MyButton_Click (...) that follows the MouseEventHandler method interface is written externally (...)
// Link our own method entity to the MyButton. Click method variable.
// In this way, when the WndProc Message Processing process of the MyButton instance is executed to the method variable Click,
// The internal OnClick function is executed first, and then the MyButton_Click () method is executed.

// When the event is defined, C # adds an event modifier, So that I cannot directly assign values to the Click by using the = operation externally, and can only operate in the + = mode.

// In fact, the real Button. Click implementation is more complex than the above mentioned. It can be said that delegate is only the core of completing the event,
// But we can implement a custom event.
// Click the fifth example to study the button and click the real internal implementation. This is a bit of a problem. It is not just the use of delegate.
}

// Test code

FourthTest. FourthTest test4 = new FourthTest. FourthTest ();
Test4.DoTest1 ();

Console. ReadLine ();
Test4.DoTest2 ();

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.