c#3.0 extension methods

Source: Internet
Author: User

extension Methods

In many cases we need to write a variety of help classes, because the official offer of the comprehensive, there will be not included in the place, then a static help class can help us solve the problem

For example, if you want to verify that the contents of a string are not null and equal to the admin but many places are to be called, according to the encapsulation idea to encapsulate a method, it may look like the following

This is not a problem, the code is relatively simple, but it is difficult to have a sense of closeness, because Stringihelper.validarg such a call exists. For example, this validation should be taken into account by the authorities, but they neglect to write such an extension with a deep sense of intrusion.

Using the extension method, you can make the method costume a self-bringing method of the string type. Like this.

what have we changed compared to the previous? The parameters in the Validarg method are added with a this keyword, called when the direct str. Validarg, which looks cool and magical, looks just like the native method provided by string! But the truth is, not that the translator tells us that it is an extension method

How does the compiler know? What has been done behind the call? In fact, it is not as complicated as it is, and the compiler looks for the instance method first, which is the namespace of the current instance type, and if there is no instance method, it will find all the extension methods under the current using namespace and the current space, what if it is an extension method? Please look

after the anti-compilation saw there is a The System.Runtime.CompilerServices.ExtensionAttribute feature is that the compiler scans all the methods that add the ExtensionAttribute feature as extension methods

and the call, and finally the use of the Stringhelper.validarg Such a method of invocation,

The following need to say about the use of extension methods and their limitations, the Declaration of extension methods to comply with the following rules

    • It must be in a non-nested, non-generic static class
    • Method has at least one parameter
    • The first parameter must be prefixed with the This keyword
    • The first argument may not have any other modifiers
    • The type of the first parameter cannot be a pointer type

with the extension method, we can extend the use of the third-party class library to make it more powerful! And it's very natural to use. Managing extension methods is a good experience, and the most effective way to manage them is by using namespaces. For example enumerations, namespaces can use enumextension.

c#3.0 extension methods

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.