C # delegation of basic concepts

Source: Internet
Author: User

Delegation is a very important concept in C # and has been widely used in C #, such as events and threads. So what is delegation? Specifically, delegation is a type of reference method. Once a method is assigned to the Delegate, the delegate has the same behavior as the delegate. The use of delegate methods can have parameters and return values like any other method.

Delegation has the following features:

  • The delegate is similar to a C ++ function pointer, but it is type-safe.

  • The delegate allows passing methods as parameters.

  • A delegate can be used to define a callback method.

  • The delegate can be linked together. For example, multiple methods can be called for an event.

  • The method does not need to be exactly matched with the delegate signature. For more information, see covariant and inverter.

  • C #2.0 introduces the concept of anonymous methods, which allow passing code blocks as parameters instead of Individually Defined methods.

There are three steps to use delegation in C:

1. Define delegation:

// Declare the delegate
Public delegate void MyDel ();

2. instantiate the delegate:

TestDel t = new TestDel ();
Console. WriteLine ("----- The following is a simple demo using delegation --------");
// T. MyMethod ();

/// Instantiate the delegate and use a method to instantiate it
/// The method signature must be consistent with the delegate Signature
MyDel del = new MyDel (t. MyMethod );

3. Call the delegate:
/// Call the delegate
Del ();

Well, in fact, the delegate changes are very complicated, but they basically follow these three steps. As you have said, let's take a look at the complete code.

NamespaceDelegateDemo
{
//Delegate Declaration
Public Delegate VoidMyDel ();
//Declare a delegate with Parameters
Public Delegate VoidMyDel2 (IntNum1,IntNum2 );
//Declare a delegate with a return value
Public Delegate StringMyDel3 (StringS );

//The Declaration delegate is used to demonstrate the anonymous method.
Public Delegate StringProcessString (StringS );

ClassProgram
{
Static VoidMain (String[] Args)
{
# RegionDelegated demo


/*
TestDel t = new TestDel ();

# Region simple instantiation delegate and call delegate
Console. WriteLine ("----- The following is a simple demo using delegation --------");
// T. MyMethod ();
/// Instantiate the delegate and use a method to instantiate it
/// The method signature must be consistent with the delegate Signature
MyDel del = new MyDel (t. MyMethod );
/// Call the delegate
Del ();

// After C #2.0, the delegate can be instantiated in this way.
MyDel del4 = t. MyMethod;
Del4 ();

// Instantiate using static methods
Del4 = TestDel. MyStaticMethod;
Del4 ();

// The following code has the same effect
// MyDel2 del2 = new MyDel2 (t. MyMethod );
// Del2 (10, 20 );
MyDel2 del2 = t. MyMethod;
Del2 (10, 20 );

// MyDel3 del3 = new MyDel3 (t. MyMethod );
// Console. WriteLine (del3 ("abc "));
# Endregion

# Region anonymous method instantiation delegate
Console. WriteLine ("----- The following is an anonymous demonstration --------");
// Instantiate the delegate using an anonymous method
ProcessString p = delegate (string inputString ){
Return inputString. ToUpper ();
};
// Call the anonymous method by Delegate
Console. WriteLine (p ("aaaa "));
# Endregion

# Region commissioned multicast demonstration

Console. WriteLine ("----- The following is a demo of delegated multicast --------");
MyDel mydel1 = t. MyMethod;
MyDel mydel2 = t. MyMethod2;
MyDel mydel3 = TestDel. MyMethod3;
MyDel allMyDel = mydel1 + mydel2 + mydel3;
AllMyDel ();
AllMyDel-= mydel3;
AllMyDel ();

# Endregion

# Region delegate as parameter demonstration

Console. WriteLine ("------- The following is a delegate as a parameter demonstration ------");
MyDel3 paramMyDel3 = t. MyMethod;
TestDel. MyParamMethod ("aaa", paramMyDel3 );

# Endregion

# Region delegate as return value

Console. WriteLine ("--- The following is a demo of delegate as return value ------");
/// ReturnMyDel indicates the return value of t. MyReturnMethod ()
MyDel3 returnMyDel = t. MyReturnMethod ();
/// ReturnMyDel points to t. MyMethod
// MyDel3 returnMyDel = t. MyMethod;

Console. WriteLine (returnMyDel ("sssssssssssss "));

# Endregion
*/
# Endregion

//MyReturnDelegateTest my = new MyReturnDelegateTest ();
//My. MyTest ();
MyParamDelegateTest myParam= NewMyParamDeleg

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.