C # Extension Methods extension method

Source: Internet
Author: User

The new features introduced in C #. NET Framewoke 3.0 provide extensions. NET class method , can increase the aesthetics of the Code!


There are several requirements for writing an extension method:


The class in which the extension method resides must be global, not an inner nested class, and the class of the extension method must be a static class .

The extension method must be a static method .

The data type of the first parameter of the extension method must be the type to be extended and the This keyword is used.


Extension method Definition:

public static class cexlongmethed{    //No parameter no return value public    static void Print (this long L)    {        Console.WriteLine ( l);    }    The parameter has a return value    of public static long Add (this long l, long L2)    {        return (L + L2);}    }


Call:

Long lval = 1;lval.print (); Console.WriteLine (Lval.add (2));

C # Extension Methods extension method

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.