[Original] basic Extension Method usage, original extension method usage
Preface
First, let's take a look at the description above msdn: the extension method enables you to "add" methods to existing types without creating new derived types, re-compiling, or modifying the original types in other ways.
In fact, we can't modify the class libraries written by the students of Microsoft, but we can add some of our methods to these class libraries in one way.
1.0 application scenarios of extension methods
DateTime now = DateTime. Now; // 1.0 format now to yyyy-MM-dd and output string fmtstr = now. Tostring ("yyyy-MM-dd ");
// 2.0 format now to yyyy-MM-dd HH: mm: ss output
String fmtstr1 = now. Tostring ("yyyy-MM-dd ");
Now there is only one problem here. If we need to use thousands of times in our system? What should we do at this time? Clever, you must have come up with the idea of writing a method...
public static class ExtHelper { public static string fmtDatayyyymmdd(DateTime now) { return now.ToString("yyyy-MM-dd"); } public static string fmtDatayyyymmddhhmmss(DateTime now) { return now.ToString("yyyy-MM-dd HH:mm:ss"); } }
After transformation, the above Code can be rewritten as follows:
DateTime now = DateTime. now; // 1.0 format now to yyyy-MM-dd and output string fmtstr = ExtHelper. fmtDatayyyymmdd (now); // 2.0 format now to yyyy-MM-dd HH: mm: ss and output string fmtstr1 = ExtHelper. fmtDatayyyymmddhhmmss (now );
But programmers are very lazy, so we must remember this ExtHelper. Think about whether we have a better way, if we can now. the method name is good, and the extension method can meet our requirements...
2.0 three elements of the Extension Method
Steps for writing the extension method:
1. It must be placed in a static class.
2. The method must be a static method.
3. The first parameter of the method is a type parameter, which starts with this.
Transformed ExtHelper class
public static class ExtHelper { public static string fmtDatayyyymmdd(this DateTime now) { return now.ToString("yyyy-MM-dd"); } public static string fmtDatayyyymmddhhmmss(this DateTime now) { return now.ToString("yyyy-MM-dd HH:mm:ss"); } }
The call is as follows:
DateTime now = DateTime. now; // 1.0 format now to yyyy-MM-dd and output string fmtstr = now. fmtDatayyyymmdd (); // 2.0 format now to yyyy-MM-dd HH: mm: ss and output string fmtstr1 = now. fmtDatayyyymmddhhmmss ();
3.0 features of the Extension Method
1. The priority of the extension method is lower than that of the instance method.
public static class ExtHelper { public static string ToUpper(this string str) { return str.ToUpper(); } }
We all know that there is a ToUpper Method on string. If we define the same extension method at this time, this method will not work...
2. The extension method can be overloaded with the instance method.
public static class ExtHelper { public static string ToUpper(this string str, int num) { return str.ToUpper() + num; } }
In this case, if we have our own business logic, we can reload it with the instance method...
3. The extension method defined on the parent class can be used on the subclass object.
Public interface IPig {} public class Pig: IPig {private string _ name; public string Name {get {return _ name;} set {if (value. length >=2) {_ name = value;} else {// throw an exception }}// automatic attribute public int Age {get; set ;}} public class smallPig: pig {}
public static class ExtHelper { public static string Get(this Pig pig) { return pig.Name; } }
Subclass can also point out the Extension Method
var pig = new Pig(); pig.Get(); var spig = new smallPig(); spig.Get();
4. The extension method defined on the interface can be used on implementation class objects (EF and MVC are frequently used)
public static class ExtHelper { public static void Set(this IPig ipig) { } }
The interface implementation class can also point out the expansion method. The Extension Method in MVC in IQueryable <T>, used in Dbset <T>
var pig = new Pig(); pig.Get(); pig.Set(); var spig = new smallPig(); spig.Get(); spig.Set();
If you feel good after reading this article, click 【Follow] To support the blogger. Thank you!
If you feel good after reading this article, click
[Recommended]
Author:Feng Xiaoyi
QQ: 616931
Source:Http://www.cnblogs.com/fenglingyi
Statement:The copyright of this article is shared by the author and the blog Park. This statement must be retained without the author's consent and the original article connection must be clearly provided on the article page. Otherwise, the legal liability will be retained.