ASP. NET multi-thread programming-multi-thread programming under. NET (5)

Source: Internet
Author: User

Reading directory

I. Delegation and thread

Ii. Delegated instance

Iii. Running Effect

I. Delegation and thread

. Commission Basis
-Purpose of delegation: To pass functions as parameters
-Similar to function pointers in C ++, function pointers are different from function pointers: function pointers can only reference static methods, while delegation can reference static methods or instance methods, when a delegate references an instance method, the delegate not only stores the reference to the method entry point, but also stores the instance reference to call this method.
-Is the basis for event processing.
-Delegate Declaration: delegate int MyDelegate (int I); int indicates the function return type, MyDelegate indicates the delegate name, And I indicates the function parameter.

Ii. Delegated instance

1: declare Delegation

Delegate int MyDelegate (int I );

2: Define a static method and return the product of two numbers

Public static int DelegateMethod (int I)

{
Return I * I;

}

3: declare a delegate variable mydelegate and bind it to the static method DelegateMethod.

MyDelegate mydelegate = new MyDelegate (DelegateMethod );

Instance

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

Namespace _ 3_DelegateUse
{
Class Program
{
// Step 1: Delegate www.2cto.com
Public delegate int MyDelegate (int I );
Static void Main (string [] args)
{
// Step 3 declare a delegate variable mydelegate and bind it to the static method DelegateFunction
MyDelegate mydelegate = new MyDelegate (DelegateFunction );
Console. Write ("enter a number :");
Int I = Int32.Parse (Console. ReadLine ());
// Call the delegate method DelegateFunction
Int intResult = mydelegate (I );
Console. WriteLine ("Result:" + intResult );
Console. ReadLine ();
}

// Step 2 defines a static method and returns the product of two numbers.
Public static int DelegateFunction (int I)
{
Return I * I;
}
}
}

Iii. Running Effect

 


Learn a little bit every day, make a little progress every day, record work with words, and record life with words

From childhood sleepy

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.