C #4.0 syntactic sugar Article 4: extension methods

Source: Internet
Author: User

C #4.0 syntactic sugar Article 4: extension methods
Today, I will continue to share the C #4.0 syntax sugar extension method, which is my favorite method. Let's first think about what we will do if the original type we previously wrote cannot meet our current needs, but we need to add new methods to this type to implement the current situation. Let me first talk about the method I did not learn this knowledge point before: the most stupid way is to modify the original type, and then add a method to meet the needs of the change, copy code 1 public class KuozFF 2 3 {4 5 public void NormalMethod () 6 7 {8 9 Console. writeLine ("I am the original method"); 10 11} 12 13 public void ExtensionMethod () 14 15 {16 17 Console. writeLine ("I'm an extension method"); 18 19} 20 21} copy code 1 KuozFF method = new KuozFF (); 2 3 method. normalMethod (); 4 5 method. extensionMethod (); 6 7 Console. readLine (); but for example, if someone else gives you a dll file, you can't modify it, but you want What should I do if I add your method to this class? Write a class by yourself and the class inherits from the original class. The Code is as follows: Copy code 1 public class KuozFF 2 3 {4 5 public void NormalMethod () 6 7 {8 9 Console. writeLine ("I am the original method"); 10 11} 12 13 14 15} 16 17 18 19 public class MYKZFF: KuozFF20 21 {22 23 public void ExtensionMethod () 24 25 {26 27 Console. writeLine ("I'm an extension method"); 28 29} 30 31} copy the Code call code as follows: Copy code 1 MYKZFF method = new MYKZFF (); 2 3 method. normalMethod (); 4 5 method. extensionMethod (); 6 7 Console. readL Ine (); The above results show that the effect is the same, but some people do not want to write the inheritance class or modify the source code. What should I do? At this time, the extension method was born! Let's take a look at the official explanation: the extension method enables you to "add" methods to existing types without creating new derived types, re-compiling, or modifying original types in other ways. An extension method is a special static method, but it can be called like an instance method of an extension type. For client code compiled in C #, there is no obvious difference between calling extension methods and calling methods actually defined in the type. This is the explanation on Microsoft MSN. Let's look at the Code directly, what is the extension method? Copy code 1 public class KuozFF 2 3 {4 5 public void NormalMethod () 6 7 {8 9 Console. writeLine ("I am the original method"); 10 11} 12 13 14 15} 16 17 18 19 public static class ExtensionClass20 21 {22 23 public static void ExtensionMethod (this KuozFF k) 24 25 {26 27 Console. writeLine ("I'm an extension method"); 28 29} 30 31} copy code 1 KuozFF method = new KuozFF (); 2 3 Method. normalMethod (); 4 5 method. extensionMethod (); 6 7 Console. readLine (); from the code above, we can see that we do not need to worry about where to write the extension method when calling the client. As long as you instantiate the original class, the extension method will automatically have it. Extension methods are everywhere in C #4.0. Let's take a look at the built-in extension methods in C # For a deeper understanding: copy code 1 public static class Enumerable 2 3 {4 5 public static IEnumerable <TSource> Where <TSource> (this IEnumerable <TSource> source, Func <TSource, bool> predicate ); 6 7 public static IEnumerable <TSource> Where <TSource> (this IEnumerable <TSource> source, Func <TSource, int, bool> predicate ); 8 9 public static IEnumerable <TResult> Select <TSource, TResult> (thi S IEnumerable <TSource> source, Func <TSource, TResult> selector); 10 11 public static IEnumerable <TResult> Select <TSource, TResult> (this IEnumerable <TSource> source, func <TSource, int, TResult> selector); 12 13} copy the code. The above is the extension method of the Microsoft IEnumerable class. then you can see how many rich where methods, such as select, play a role in the extension method. Here, I just came up with a method for extension. Next we will write down the string class extension method. We used to judge whether a string is null or empty, and used the built-in system method string. isNullOrEmpty (s). Let's make this method an extension method to demonstrate: Copy code 1 public static class Demo1 2 3 {4 5 public static bool IsNullOrEmpty (this string s) 6 7 {8 9 return string. isNullOrEmpty (s); 10 11} 12 13} copy the Code call code as follows: copy the Code 1 string temp = "12"; 2 3 bool result = temp. isNullOrEmpty (); 4 5 Console. writeLine (result); 6 7 Console. readLine (); copy the code. From the call code, we can see that the string itself does not have IsNullOrE. The mpty () method is available through our own write extension method.

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.