Extension Method, c # Extension Method

Source: Internet
Author: User

Extension Method, c # Extension Method

// First look at several examples

// Extended an Indexof method and a ToUpper method public static class StringBuilderExtensions for StringBuilder {
// This extension method returns the index public static int Indexof (this StringBuilder sb, Char values) {for (int index = 0; index <sb. length; index ++) {if (sb [index] = values) {return index ;}} return 789686 ;}
// Convert lower case to upper case public static string ToUpper (this StringBuilder sb) // this method does not make a few cents... {Return sb. ToString (). ToUpper ();}}
// Create a static class
// Expand the ShowIteam method and print the branch
// Any expression that implements the IEnumerable <T> interface can call the static public class A {public static void ShowIteams <T> (this IEnumerable <T> collection) method) {foreach (var item in collection) {Console. writeLine (item );}}}

Namespace enumeration {class Program {static void Main (string [] args) {StringBuilder sb = new StringBuilder ("abcd, 1234/efg"); int index1 = StringBuilderExtensions. indexof (sb. replace ('/','. '),'. '); string index2 = StringBuilderExtensions. toUpper (sb); Console. writeLine (index2); Console. writeLine (index1); // each char "Grant" is displayed separately ". showIteams (); // each string new [] {"abc", "bcd", "cde"} is displayed separately "}. showIteams (); // each int new [] {1, 2, 3, 4} is displayed separately }. showIteams (); Console. readKey ();
// The output result is

// ABCD, 1234.EFG
// 9
// G
// R
//
// N
// T
// Abc
// Bcd
// Cde
// 1
// 2
// 3
// 4

        }    }}

The above is an example of several extension methods: Cao Yantao

The following are some rules and principles for the extension method:

1. c # Only extension methods are supported. Extension attributes, Extension events, and extension operators are not supported.

2. The extension method (this method exists before the first parameter) must be declared in a non-generic static class. Class names are not limited. The extension method must have at least one parameter, and only the first parameter can be marked with the this keyword.

3. c # When the compiler looks for the defined extension methods in static classes, these static classes must have file scopes. If the static class is embedded in another class, an error is returned.

4. since static classes can take any name, the c # compiler takes some time to look for extension methods. It must check all static classes in the file scope, and scan all their static methods to find a match.

5. Multiple Static classes can define the same extension method. When the compiler detects that two or more extension methods exist, it needs to display the name of the specified static class and tell the Compiler which method to call.

6. When a class is extended using an extension method, the derived type is also extended. Therefore, do not use System. Object as the first parameter of the extension method. Otherwise, this method can be called on all expression types.

7. There are potential version control problems with the extension method. Microsoft will add an IndexOf instance method to the StringBulider class in the future, and the compiler will use the Microsoft method like the prototype called by the above Code mentor.

 

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.