extension methods in C #

Source: Internet
Author: User

What is an extension method, as explained in MSDN: The extension method enables 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.

What do you mean? When we give a DLL extension functionality, we can add the required functionality by directly modifying the source code, but there are many times when we cannot modify the source code, there is a need to implement the extension method.

The implementation of the extension method is very simple. The required conditions are:

    1. a static class
    2. static class defines a static method, which is the method we need to extend

For example:

1  Public Static classExternalmethod2     {3          Public Static intExtisrange ( This intTintMinintmax)4         {5             intresult =0;6             if(T <min)7             {8result =-1;9             }Ten             Else if(Result >max) One             { Aresult =1; -             } -             Else the             { -result =0; -             } -             returnresult; +         } -  +          Public Static stringToString ( This intt) A         { at             return "This  is a extension of the method, but never       Call"; -         } -}

The above is a typical example of an extension method that defines a static class Externalmethod defines two extension methods in the class one is the extisrange one is ToString , The first parameter in the method definition for both methods is (this int t), which means that both methods are extensions to the int type.

In the example above, we have extended the ToString method, and you should know that The ToString method is defined in the System.Object type, and the int type also overrides the method, all of which have the ToString method definition in an instance of int, at which point we do not invoke the ToString extension method, This is because if the extension type has a method with the same name as the extension method, the method defined in the type is called first, and the extension method is not called.

Invocation of extension methods

The invocation of an extension method is the same as a normal type instance call

1  Public classTestextmethod2     {3          Public Static voidMain ()4         {5             intAge = Wu;6Console.Write (age. Extisrange ( -, +));7         }8}

The integer type defined in the previous example, age calls the extension method Extisrange that was just defined, and calls the method the same way as any other integer method call.

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.