extension methods in C #

Source: Internet
Author: User

Extension methods
There are many ways to extend a class, and if you have the source code for the class, inheritance is the best way to add functionality to an object. If you do not have source code, you can use the extension method.

 Public class student{    publicint age=;      Public Static class studentextention    {        publicstaticvoid addage ( This Student stu,int i)        {            + = i;        }}     }        

The extended method must be a static method and must be placed in a static class. For an extension method, the first parameter is the type to be extended and must be placed after the This keyword. In an extension method, you can access all public methods and properties of the type being extended.

When an extension method is called, it must be an instance object call.
If the extension method is the same as the signature of a method in the class, the extension method is never called, and the instance method already in the class takes precedence.

extension methods in C #

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.