Extensions, delegates, Lambda, LINQ

Source: Internet
Author: User

1. Expansion

Extensions are a very useful feature. If you have a class, you can't modify it, and you want to add a method to him. This process is an extension. Extension is the extension method.

Example 1:

class people

     Public class people    {        publicstringgetset;}          Public int Get Set ; }    }

Add an extension:

1, the extension must be a static class

2, the extension method must be a static method

3. The extended object must be preceded by this people people

     Public Static class Extend    {        publicstatic string  ToJson ( This people people)        {            return  jsonconvert.serializeobject ( people);        }    }

Using extensions:

            /// Creating Objects            New " Eleven "  -  };             /// using the Amplified ToJson             people. ToJson ();             /// using the amplified ToJson            Extend. ToJson (people);

2. Commission

The essence of a delegate is a function pointer. Delegates are the basis of events

Define a delegate:

1, the definition of the delegate is, in front of the method plus a delegate

2, and this method is not and can not be implemented

3. And the delegate is a type

        /// <summary>        ///defines a delegate/// </summary>        /// <param name= "a" >parameter a</param>        /// <param name= "B" >parameter B</param>        /// <returns></returns>        Delegate intPlus (intAintb);

Use: (Multiple subscriptions, can be executed in parallel)

            /// Implementing a Delegate            New Plus (plusmethods);             /// Parallel implementations            Plus + = PlusMethods2            ; /// Parallel implementations            Plus + = PlusMethods3            ; /// Execute delegate, plusmethods,plusmethods2,plusmethods3 parallel execution            Plus. Invoke (12);

Actual method:

 static  int  plusmethods (int  A, int   b) {Console.Write Line (  plusmethods:{0},{1}  Span style= "color: #800000;"            > " , A, b);         return  a + b; }
 static  int  PlusMethods2 (int  A, int   b) {Console.Write Line (  plusmethods2:{0},{1}  Span style= "color: #800000;"            > " , A, b);         return  a + b; }
        Static int PLUSMETHODS3 (intint  b)        {            Console.WriteLine ("plusmethods3:{0},{1 }", A, b);             return A + b;        }

3. Lambda

Lambda is actually a syntax for an anonymous method to evolve.

For example, let's take a look at how to evolve step-by-step.

 ///  anonymous method  Plus plus2 = new  Plus (delegate  (int  A, int   b) {Console.WriteLine (  "  anonymous method {0},{1}  "  , a, b);             return  a + b;            }); Plus2. Invoke ( 1 , 2 ); 
 /// lambda method is to remove the delegate, adding a =& in the middle Gt Symbol  plus PLUS3 = new  Plus ((int  A, int  b) => {Console.wri Teline (   anonymous method {0},{1}   " , A, b);             return  a + b;            }); Plus3. Invoke ( 1 , 2 ); 
            /// Lambda method, the new Plus () is also removed            Plus PLUS4 = (intint b) = =            {                Console.WriteLine (" anonymous method {0},{1}  ", A, b);                 return A + B;            };            Plus4. Invoke (12);

LAMBDA 3 constituent structure, Parameters: () middle character = = entity {}

4. Linq

LINQ is Microsoft's move of SQL syntax into C # code

The query for the collection is helpful.

Detailed LINQ Reference:

Https://msdn.microsoft.com/zh-cn/library/bb397676.aspx

Extensions, delegates, Lambda, LINQ

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.