C #3.0 highlights-Expansion Method

Source: Internet
Author: User

"Extension Method" is a static method of static classes. We can call extension methods just like calling methods of other class instances.

Description and call of extension methods

For example, I want to add a static toint32 method for the string type and convert the string type to int32:

 
Public static class exmethod {public static int32 toint32 (this string s) {return int32.parse (s );}}

Note:

1. Add the this keyword before the first parameter to specify this extension method.

2. The extension method must be static and the class must be static.

3. The class of the extension method cannot be a "nested class" (this class is nested in another class)

Calling an extension method is simple and looks like calling a static method of a common class instance:

 
String S = "1234"; S. toint32 ();

In the Visual Studio programming environment, the extension method gives a special prompt:

Extended method priority

The priority of the conventional object instance method lies in itsDeclaration FormAndWhen the declared form of the called function is matchedHigher than the extension method.

The extension method is a very useful concept. When developers want to expand a sealed class, there is a way.

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.