"C # Review Summary" to elaborate anonymous methods

Source: Internet
Author: User

1Preface

This series summarizes the basics such as delegate, anonymous method, lambda expression, generic delegate, Expression tree, event. (I have a small white one, there is the wrong place to hope that the big guy correct)

Department Class 1: Commissioned

Department 2: An anonymous approach

Series 3: About lambda expressions

Series 4: Generic Delegates

Series 5: Elaborate expression tree

Series 6: Events in detail

Then the delegation of the previous article to discuss the anonymous method, still with the big guy's article to start:

"In the previous article has been known, complete a commissioned application in three steps to go, lack of a step is not, if you want to stride, beware of the pace of the big pull the egg." But Microsoft is not afraid to pull the egg, must take three steps to make two steps to go! So Microsoft is using an anonymous method to simplify the top three steps. Anonymous method How to say this thing, in C # is absolutely dispensable things, just for C # icing on the cake , some ingenuity to give it a name called Grammar sugar. "

2What is an anonymous method

the anonymous method is c#2.0 introduces a new feature that allows developers to declare their own function code inline (inline) without using a delegate function (delegate. Anonymous methods are usually in the 1. A temporary method is required and the method is rarely used; 2. The code for this method is very short and may even be used in cases where the method declaration is short.

3initial knowledge of anonymous methods

As we have already mentioned, a delegate is used to refer to a method that has the same label as it. In other words, you can use a delegate object to invoke a method that can be referenced by a delegate.

Anonymous Methods ( Anonymous methods) provides a technique for passing code blocks as a delegate parameter. An anonymous method is a method that has no name but a principal.

in an anonymous method, you do not need to specify a return type, which is inferred from the return statement within the method body .

4use of anonymous methods

It takes only two steps to complete:

STEP01: First define a delegate with delegate.

STEP02: Use this notation delegate (int x, int y) {return x + y;} , assigning a method to a delegate is, in fact, an anonymous method.

    classProgram {//STEP01: First define a delegate with delegate.          Public Delegate intCaculateadd (intXinty); Static voidMain (string[] args) {            //STEP02: Use this notation delegate (int x, int y) {return x + y;}, assign a method to the delegateCaculateadd ADD1 =Delegate(intXintY) {returnX +y;}; //int result = Cadd.invoke (5, 6);            intresult = ADD1 (4,5);            Console.WriteLine (result);                   Console.readkey (); }    }

It is surprising to find that this is not the three steps in front of the two steps to go wow?

5Summary

an anonymous method implements a delegate in only two steps, eliminating the step of declaring the method, which is convenient for programmers. This way, the anonymous method is almost there, and the next article is about Lambda expressions.

Friendly Tips Mhq_martin Blog Park address:http://www.cnblogs.com/mhq-martin/This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

"C # Review Summary" to elaborate anonymous methods

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.