LinQ---Extension methods and lambda expressions

Source: Internet
Author: User

LinQ---Extension methods and lambda expressions

Extension methods:

Extension methods enable you to "add" methods to an existing type without creating a new derived type, recompiling, or otherwise modifying the original type. An extension method is a special static method, but it can be called just like an instance method on an extended type. For client code written in C # and Visual Basic, there is no significant difference between calling extension methods and calling methods that are actually defined in the type.

Format:

public static class Classa{public Staticvoid Extramethod (This string s) {...}}

For example:

public static class Extraclass    {        //extension method, special static method public        static string topascal (this string s)   // This is a post-band type, and the table name adds a special method        {            return s.substring (0,1) to the type. ToUpper () + s.substring (1). ToLower ();        }}

Precautions:

    • Extension method is a special kind of static method
    • Extension methods must be defined in a static class
    • Extension methods have a lower priority than class methods with the same name
    • Extension methods are only valid within a specific namespace
    • Unless you must not abuse the extension method

Lambda expression

C#lambda Basic form of expression:(parameter list) = = {Method body}

Description

The parameter types in the argument list can be either explicit or inferred types

If it is an inferred type, the data type of the parameter is automatically inferred by the compiler based on the context

Lambda is used in method-based LINQ queries as parameters for standard query operator methods such as where and where.

Detailed Introduction

Example analysis

Public partial class Form1:form {//defines a delegate public delegate string Deletransfer (string s);        Public Form1 () {InitializeComponent (); } private void btnTest_Click (object sender, EventArgs e) {//extension method----string strtest            = "Asdsad";            Console.WriteLine (Strtest.tolower ());            Console.WriteLine (Strtest.toupper ());            Console.WriteLine (Strtest.topascal ());             Console.WriteLine ("-------------------------------------");  Lambda Source//.net FrameWork 1.0 Delegate---function pointer deletransfer trans = new Deletransfer (topascal);             Delegate-Pointing Method Topascal Console.WriteLine (trans ("ABCDEFGH")); The. NET 2.0 anonymous method Deletransfer Transs = Delegate (string s) {return s.substring (0,1). ToUpper () + s.substring (1). ToLower ();            };             Console.WriteLine (Transs ("abcdefgh")); . NET 3.5 anonymous Method//deletransfertransss = (s) = = (S.Substring (0, 1). ToUpper () + s.substring (1).            ToLower ()); Deletransfer transss = s =>s.substring (0, 1). ToUpper () + s.substring (1).            ToLower ();        Console.WriteLine (Transss ("abcdefgh")); }//Convert the first letter of the string to uppercase the public string topascal (string s) {return s.substring (0,1). ToUpper () + s.substring (1).        ToLower ();   }} public static class Extraclass {//extension method, special static method public static string Topascal (this string s) This is a post-band type, and the table name adds a special method {return s.substring (0,1) to the type. ToUpper () + s.substring (1).        ToLower (); The public static string topascal (this string s, int len)//this a post-band type, and the table name adds a special method to the type {return s.sub String (0,1). ToUpper () + s.substring (1, Len).        ToLower () + s.substring (len + 1); }}


Learn to start sharing (Share link: http://pan.baidu.com/s/1AQgHo)

LinQ---Extension methods and lambda expressions

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.