[C #] C # basic review,

Source: Internet
Author: User

[C #] C # basic review,
C # basic review-anonymous method directory

  • Introduction
  • Use range of parameters for anonymous methods
  • Delegation example

 

Introduction

In versions earlier than C #2.0, we create the unique form of delegation-naming method.

C #2.0 -- the anonymous method is introduced. In a version ≥c #3.0, we replace the anonymous method with a Lambda expression, lambda expressions are also the preferred method for writing Inline code because they are more concise.

 

The anonymous method is, as the name implies, the anonymous method is a method without a name. The most obvious advantage of the anonymous method is that it can reduce the workload of writing another method. Another advantage is that it can directly access the caller's variables, thus reducing the complexity of passing parameters.

The anonymous method is not a predefined method, but a delegated code block. It is no different from a common method in use, however, the anonymous method can reduce system overhead to a certain extent.

Anonymous method, Keyword: delegate.

Anonymous method, Application Scenario: Generally, ① A temporary method is required, and the method is rarely used; ② the code of this method is very short, not long.

  

[Note] the anonymous method provides functions not available in Lambda expressions. That is, you can ignore the parameter list using the anonymous method. This means that the anonymous method can be converted to a delegate with various signatures, which is almost impossible for Lambda expressions.

 

To pass a code block as a delegate parameter, using the anonymous method is currently the only method.

Example 1:

// Create a Click event button1.Click + = delegate (Object o, EventArgs e) {MessageBox. Show ("Click! ");};

 

Example 2:

// Create a delegate. delegate void MyDel (int x); // use the anonymous method and instantiate the delegate MyDel del = delegate (int k ){/*... */};

 

The anonymous method reduces the coding overhead required to instantiate the delegate in the preceding two examples because no additional method is required.

For example, if the system overhead required to create a method is not necessary, it is very useful to specify a code block (rather than a delegate.

 

Here I will use an example to demonstrate how to create and start a new thread in the form of an anonymous method:

// Example of creating a thread to start
Void Start (){
// Declare Thread thread = new Thread (delegate () {Write ("Hello,"); WriteLine ("Fanguzai! ");});

// Start thread. Start ();}

 

Use range of parameters for anonymous methods

The range of parameters used for anonymous methods: anonymous method blocks.

If the target is outside the block, using the -- jump keyword (similar to goto/break/continue) inside the anonymous block is incorrect. If the target is inside the block, you use the -- jump keyword outside the anonymous block (similar to goto/break/continue), which is also incorrect.

If the range of local variables and parameters already contains anonymous method declarations, the local variables and parameters are called the "external" (external) variables of the anonymous method. You see,numIs an external (external) variable:

        int num = 250;        MyDel del = delegate() { WriteLine("#:{0}", --num); };

  

Referenced by this external variableNum, WillIt is considered to be captured when a delegate is created. It is different from the common local variable, the lifetime of this external variable-the delegate object that references this anonymous method is.. NET.

 

[Note] anonymous methods cannot access parameters with the ref and out keywords within the external (external) range.

[Note] unsafe Code cannot be accessed in anonymous blocks.

[Note] on the left of the is operator, the anonymous method cannot be used.

 

Delegation example

In the following example, both the anonymous method and the naming method are used to call the delegate:

// Declare the delegate void MyDel (string s); class MyClass {static void Main () {// use the anonymous method to instantiate the delegate MyDel p = delegate (string msg) {Console. writeLine (msg) ;}; // directly call p ("Call the delegate using an anonymous method. "); // P = Console. writeLine; Console. writeLine ("abbreviated form above"); // use the delegate instantiation of the static method of "Do. P = new MyDel (MyClass. Do); // use the old style to call the delegate p ("Use the naming method to call the delegate. "); Console. Read () ;}// used to delegate subsequent calls to static void Do (string msg) {Console. WriteLine (msg );}}

C # basic Review Series

C # knowledge Review-serialization

C # knowledge Review-Expression Tree Expression Trees

C # knowledge Review-feature Attribute and AssemblyInfo. cs-understanding common feature attributes

C # knowledge Review-delegated delegate and C # knowledge Review-delegated delegate (continued)

C # knowledge Review-Introduction to events and C # knowledge Review-Event Events

There is no unspeakable secret between the string, the String, the big S, and the small S

C # knowledge Review-do you really understand exceptions?

Have you ever understood the entry function Main? Using batch processing to convert the Main function

 

 

[Blogger] Anti-Bot

[Source] http://www.cnblogs.com/liqingwen/p/6216494.html

[Reference] Microsoft official documentation

 

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.