"C # Advanced Programming" Learning notes

Source: Internet
Author: User

    • 3.8 Extension methods

An extension method is a static method, which is part of a class, but is not actually placed in the source code of the class. For some reason, the original source code of the assembly cannot be modified directly. All the work you have to do at this point is to create a static class

1 namespaceWrox2 {3      Public Static classmoneyextension4     {5          Public Static voidAddtoamount ( ThisMoney Money,decimalAmounttoadd)6         {7Money. Amount + =Amounttoadd;8         }9     }Ten}

Note the parameters of the Addtoamount () method. For an extension method, the first parameter is the type to be extended, which is placed after the This keyword. This tells the compiler that this method is part of the money type. In this example, money is the type to be extended. In an extension method, you can access all public methods and properties of the extended type.

"C # Advanced Programming" Learning notes

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.