C #3.0 Extension Method

Source: Internet
Author: User

Expansion solution:In the past, existing class libraries were extended, and feasible methods were to directly modify or derive the source code.

Notes for extension methods:

  1. The extension method must be defined in a static class, and the extension method itself must be a static method;
  2. The first parameter in the extension method must be this, followed by the name of the class to be extended;
  3. An extension method can be called by an object instance or a static class name.

Extended method scope:

Instance call:

Namespace ExtensionMethodObjects {// 1, defines a static class public static class ExtensionMethods {// 2, defines a static method, which extends the object. /** Parameters of the extension method: the first parameter must be THIS * and then follow the type name and ID value to be extended */public static void SayHello (this object obj) {Console. writeLine ("extension method! ");}}}
Namespace ExtensionMethodObjects {class Program {static void Main (string [] args) {// instantiate an object of the object type object o = new object (); o. sayHello ();}}}

After the method is extended, you do not need to make any changes to the original assembly.

 

Static call:

Namespace InvokeExtensionMethods {// note the class visibility level, which must be visible throughout the application. Public static class ExtensionMethods {// extends the int type and adds an integer reversal capability to it. Public static int ReverseDigits (this int I) {char [] digits = I. toString (). toCharArray (); Array. reverse (digits); string newDigits = new string (digits); return int. parse (newDigits);} // extend the Car class to add a SpeedDown method public static int SpeedDown (this Car car) {// This will generate a compilation error // return -- Speed; // the correct method should be car. speed; return -- car. speed ;}}}
Namespace InvokeExtensionMethods {class Program {static void Main (string [] args) {int I = 123456; // call the instance method and directly use the extended class object instance to call the Console. writeLine ("instance method call, value after reversal: {0}", I. reverseDigits (); // call the static method. Call the static Class Name of the extension method and add the extension method to call the Console. writeLine ("static method call, value after reversal: {0}", ExtensionMethods. reverseDigits (I) ;}// defines a simple vehicle class public class Car {public int Speed; public int SpeedUp () {return ++ Speed ;}}}

We recommend that you use instance method calls. The compiler actually compiles instance calls into static method calls.

Expansion Method of the mine area:

  • The name of the extension method cannot be the same as that of the extended class. Otherwise, the extension method cannot be called.

During compilation, the priority of the extension method is always lower than the instance method defined in the type itself. In fact, this means that there is an extension method with the same signature in the class, the compiler is always bound to this instance method. When the compiler needs to call a method, it first looks for a matching method in this type of instance method. If no matching method is found, the compiler searches for any extension method defined for this type and binds it to the first extension method found.

  • You cannot directly access the Extension Method in the extension method.Extended class member variablesBecause the extension method is a static method and does not belong to the standard method in the class member, you cannot directly access it. See static call in the above example.
  • If the extension method has a different namespace than the extended method, you must reference the namespace that defines the extension method when calling the 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.