C # This

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 by the system...:. string 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 the following using command to include the wordcount Extension Method in the range:

Using extensionmethods;

In addition, the extension method can be called using the following syntax in the application:

String S = "Hello extension methods ";
Int I = S. wordcount ();

In the code, you can use the instance method syntax to call the 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.

General principles
Generally, we recommend that you implement the extension method only when you have to, and implement it with caution. If possible, client code of the existing type must be extended by creating a new type derived from the existing type. For more information, see inheritance (C # programming guide ).

When you use an extension method to expand a type that you cannot change its source code, you need to bear the risk that changes in this type implementation will cause the extension method to become invalid.

If you implement the Extension Method for a given type, remember the following two points:

If the extension method has the same signature as the method defined in this type, the extension method will never be called.

The extension method is put into a range at the namespace level. For example, if you have multiple static classes containing extension methods in the same namespace named extensions, all these extension methods are put in the range by the using extensions; command.

Class Library implementers should not use extension methods to avoid creating new versions of the Assembly. If you want to add important new features to the library and you have source code, you should follow the standard. NET Framework Assembly version control guidelines.

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.