. NET: Talking about extension methods in C #

Source: Internet
Author: User
Tags static class
The extension method (Extension Methods) is a new feature introduced in c#3.0, and is believed to have been heard and used by many people, most commonly in LINQ.

Not only that, in development, we can also create our own extension methods, which are used to optimize the design of the class and simplify the code. This article will briefly describe the concepts, definitions, usage scenarios, and points to note for extension methods.

The concept extension method is a special type of static method. For a C # type, such as a class (including a sealed class), a value type, an interface, and so on, the extension method can provide a new member for its instance without changing the source of that type. Therefore, to add accessibility to a type of framework or a third-party library, it is easy to implement it by extending the method, which is what it means to extend. Second, how to define how to create an extension method is simple, there are several steps:
1, create a static class;
2, in which a static method is created;
3, add at least one parameter for this static method, and precede the first argument with the This keyword, this keyword will tell the compiler that the current method is an extension method. This method becomes the new member of the type to which the first parameter belongs.
The following is a typical extension method that provides a way for an enumeration value to get its DescriptionAttribute attribute value:

Note: This can only be used if the namespace of the static class where the extension method resides is referenced, otherwise the direct call compiles unsuccessfully. In the example above, the extension method is used to reference the TLA.Infrastructure.Extensions namespace.

Third, when to use from the concept of extension methods, it is not difficult to see, it can be used in the following situations:
1, to extend the functionality for a type, but there is no source code, such as a framework or a third-party library of a class; for example, to get all the odd items in a list, you can add an extension method to the Ilist<t> interface, where the Ilist<t> interface itself is. The interface in the NET Framework.

2, even if you can access the original type of source code, you can also use the extension method for it to add auxiliary functions;

3, reuse code, make the code more concise; Because the extension method encapsulates a complete logic, the use of extension methods avoids copying and pasting the code. The content of the extension method in the example above also conforms to this usage scenario.

Iv. considerations The following are some considerations and best practices for defining and making extension methods:
1, the extension method is essentially to provide accessibility to the original type, so at the time of creation, to ensure that it has practical significance, and adhere to the principle of single responsibility, that is, the extension method can not be overused and it is able to complete a specific, complete function;
2, the extension method itself has universality, therefore, it should avoid the specific business data type and its related logic;
3. If the extension method is added for the interface, the namespace of the extension method can be consistent with the interface, otherwise, it should avoid writing in the same namespace as the original type, which will "pollute" the original type. The recommended practice is to set a separate namespace for the class in which the extension method resides, such as: <company>.<product>. Extentions. However, there are drawbacks to this: When manipulating an instance of an existing type, it is not easy to find the namespace where the extension method resides, and the solution to this problem is to document the extension method as much as possible and tell the other developers of the project team;
4, after adding the extension method to the interface, all classes that implement this interface will contain the extension method;
5, in the extension method, the first parameter should be non-null check, if empty, should throw ArgumentNullException (parameter is empty) exception.
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.