C # getting started with extension methods

Source: Internet
Author: User

Extension methods are defined as static methods, but they are called through the instance method syntax. Their first parameter specifies the type of the method to act on, and the parameter is prefixed with the this modifier. The extension method is in the range only when you use the using command to explicitly import the namespace to the source code. The following example shows an extension method defined for the systemstring class. Note that it is defined inside non-nested and non-generic static classes: namespace extensionmethods {public static class myextensions {public static int wordcount (this string Str) {return Str. split (New char [] {'', '. ','? '}, Stringsplitoptions. removeemptyentries ). length ;}} you can use this using command to place the wordcount Extension Method in the range: Using extensionmethods; and you can use the following syntax to call the extension method from the application: string S = "Hello extension methods"; int I = S. wordcount (); in the code, you can use the instance method syntax to call this extension method. However, the intermediate language (IL) generated by the compiler converts the code into a call to a static method. Therefore, it does not really violate the encapsulation principle. In fact, extension methods cannot access private variables in their extended types.
Generally, you call extension methods more often than implement your own extension methods.Because extension methods are called using the instance method syntax, they can be used from client code without any special knowledge.To enable extension methods for a specific type, simply addUsing command.For example, to use the standard query operatorAdd the using command to the Code:

You can use the extension method to extend the class or interface, but you cannot override the extension method.Extension methods with the same name and signature as interfaces or class methods will never be called.During compilation, the priority of the extension method is always lower than the instance method defined in the type itself.In other words, if a type hasProcess (int I) method, and you have an extension method with the same signature, the compiler always binds to this instance method.When the compiler encounters a method call, it first looks for a matching method in the instance method of this type.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 it finds.The following example shows how the compiler determines the extension method or instance method to bind.

Namespace defineimyinterface {using system; public interface imyinterface {// any class that implements imyinterface must define a method // that matches the following signature. void methodb () ;}/// define extension methods for imyinterface. namespace extensions {using system; using defineimyinterface; // The following extension methods can be accessed by instances of any // class that implements imyinterface. public static class extension {public static void methoda (this imyinterface myinterface, int I) {console. writeline ("extension. methoda (this imyinterface myinterface, int I) ");} public static void methoda (this imyinterface myinterface, string s) {console. writeline ("extension. methoda (this imyinterface myinterface, string s) ");} // This method is never called in extensionmethodsdemo1, because each // of the three classes A, B, and c implements a method named methodb // that has a matching signature. public static void methodb (this imyinterface myinterface) {console. writeline ("extension. methodb (this imyinterface myinterface) ") ;}}// define three classes that implement imyinterface, and then use them to test // The extension methods. namespace extensionmethodsdemo1 {using system; using extensions; using defineimyinterface; Class A: imyinterface {public void methodb () {console. writeline (". methodb () ") ;}} Class B: imyinterface {public void methodb () {console. writeline ("B. methodb () ");} public void methoda (int I) {console. writeline ("B. methoda (INT I) ") ;}} Class C: imyinterface {public void methodb () {console. writeline ("C. methodb () ");} public void methoda (Object OBJ) {console. writeline ("C. methoda (Object OBJ) ") ;}} class extmethoddemo {static void main (string [] ARGs) {// declare an instance of Class A, Class B, and class C. a A = new A (); B = new B (); C = new C (); // For A, B, and C, call the following methods: // -- methoda with an int argument // -- methoda with a string argument // -- methodb with no argument. // A contains no methoda, so each call to methoda resolves to // The extension method that has a matching signature. a. methoda (1); // extension. methoda (object, INT). methoda ("hello"); // extension. methoda (object, string) // A has a method that matches the signature of the following call // to methodb. a. methodb (); //. methodb () // B has methods that match the signatures of the following // method CILS. b. methoda (1); // B. methoda (INT) B. methodb (); // B. methodb () // B has no matching method for the following call, but // class extension does. b. methoda ("hello"); // extension. methoda (object, string) // C contains an instance method that matches each of the following // method CILS. c. methoda (1); // C. methoda (object) C. methoda ("hello"); // C. methoda (object) C. methodb (); // C. methodb () }}/ * output: extension. methoda (this imyinterface myinterface, int I) extension. methoda (this imyinterface myinterface, string s). methodb () B. methoda (int I) B. methodb () extension. methoda (this imyinterface myinterface, string s) C. methoda (Object OBJ) C. methoda (Object OBJ) C. methodb ()*/

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.