C#3.0 Series: Extension method

Source: Internet
Author: User
Tags extend instance method static class


We say that some of the new features are introduced in c#3.0, but the individual thinks extension method is the most exhilarating and innovative.



It really solves that by extending the existing type intact, you can add the required method members to the definition without making any changes to the type. Let me tell you the following.



Before c#3.x out.



We all know that JavaScript has a new prototype, like the extension method in c#3.x. There's not much going on here.



We mainly look. NET's implementation. We can extend the type before the c#3.x comes out.



Interface's situation:



1public interface IEmployee
2{
3    string Name { get; set; }
4    int Age{get; set;}
5    int add(int n);
6}



Extend the interface to add an Add method to perform the associated operation. Our only solution is to add an add member directly to this interface. Above Implementing this interface type must implement all the methods of the interface. So, we added the Add method, which will cause all the definitions and compilations of the type to implement it, and in many cases we don't need that at all.



Class in case:



If we add a class as the base class, adding an Add method to the base class, all the child classes will not be affected. But in many cases, we can't make any changes to the interface or class we need to expand. For example, we want to extend the DataGrid control. Our common approach is to customize a class to inherit this DataGrid, to define the members that need to be added in our own defined class, which is what we often say about custom controls, and what if for a sealed class? What we are asking for is an extension of this instance type, which means that the type of the variable cannot be changed with the object we add.



If you hear such a request: we want to extend a type or interface, but we do not allow us to modify it. This requirement is indeed a bit harsh. But we can choose extension method in c#3.x. Extension method is essentially a static function that can be invoked on an extended object instance, not an inheritance, so unlike ordinary member functions, extension functions cannot directly access the members of the extended object. can only be accessed through an instance of this object.



After c#3.x out,



Simply put, extension method is a special static method defined in the static class. The reason why this static method is special is that it can be invoked not only according to the syntax of extension, but also in the syntax of instance method.



Let's look at the examples first:



public static class MyExtensionMethods
   {
     // this代表扩展方法应用于string类型上
     public static int ToInt32(this string s)
     {
       int i;
       Int32.TryParse(s, out i);
       return i;
     }
   }
public static void fnExtensionMethod()
     {
       string s = "27";
       // 使用string的ToInt32()扩展方法
       int i = s.ToInt32();
     }



We can look at the example above, we know that string is a sealed type in the NET Framework and we can only extend it using extension method. We can look at how it is defined. ToInt32 is a static method. Unlike the general static method, a This keyword is added before the first parameter. This is the keyword that is introduced in C # 3.0 to define extension method. Adding such a keyword means that when the method is invoked the argument that this tag has this can be instance, which allows us to invoke the Static method in a way that invokes the general methods. Note: You need to use the This modifier before the first parameter (only required).





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.