C #4.0 syntactic sugar Article 4: extension methods

Source: Internet
Author: User

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 explain how I did not learn this knowledge point:

The most stupid way is to modify the original type, and then add a method to achieve the required change, as shown in the following 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}
View code

Call method:

1 KuozFF method=new KuozFF ();2 3             method.NormalMethod();4 5             method.ExtensionMethod();6 7             Console.ReadLine();
View code

The output result is as follows:

For example, if someone else gives you a DLL file, you cannot modify it. But what if you want to add your method to this class?

Write a class by yourself and the class inherits from the original class. The Code is as follows:

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}
View code

The call code is as follows:

1 MYKZFF method=new MYKZFF();2 3             method.NormalMethod();4 5             method.ExtensionMethod();6 7             Console.ReadLine();
View code

The effect is as follows:

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:

Extension Method: allows you to "add" methods to an existing type without creating a new derived type, re-compiling, or modifying the original type 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 written in C #, there is no significant 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 and what the extension method looks like:

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}
View code

The call code is as follows:

1 KuozFF method=new KuozFF();2 3             method.NormalMethod();4 5             method.ExtensionMethod();6 7             Console.ReadLine();
View code

Output result:

From the code above, we can see that we do not need to worry about where the extension method is written when calling the client. You only need to instantiate the original class and 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:

 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>(this 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 }
View code

The above is the extension method of the Microsoft ienumerable class, so we usually use the method after the dot. Then we can see how many rich where, select and other methods are 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:

 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     }
View code

The call code is as follows:

1     string temp = "12";2 3             bool result = temp.IsNullOrEmpty();4 5             Console.WriteLine(result);6 7             Console.ReadLine();
View code

Output result:

From the call code, we can see that the string itself does not have the isnullorempty () method. We can use this method to write the extension method by ourselves.

You can write your own methods or extensions to the system class to facilitate future coding.

Write it here today! Thank you for your support!

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.