Today, let's talk about the extension method of C #.
A new feature-extension method is provided for us in C # 3.0. What is an extension method? Let's take a look at MSDN annotations:
Extension methods enable you to "add" methods to existing types without creating new derived types, recompiling, or otherwise modifying the original type
That is, we can add extension methods for the underlying data types, such as: string,int,datarow,datatable, or you can add extension methods to custom classes.
So, how do we extend to existing types? Let's look at the definition of MSDN:
Extension methods are defined as static methods, but they are called through the instance method syntax. Their first argument specifies which type the method acts on, and the parameter is prefixed with the This modifier.
As you can see from the definition, to extend an existing type, you need to create a new static class, define a static method, and the first parameter of the method specifies the type with this.
Let's take a look at the example, first, I define a static class Objectextension, and then extend a tonullstring method for all objects. Returns a string if the object is empty. Empty
Otherwise, it returns obj. ToString ().
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.ComponentModel;usingSystem.Reflection;namespaceamway.oa.ms.common{ Public Static classobjectextension { Public Static stringTonullstring ( This Objectobj) { if(obj = =NULL ) { return string. Empty; } returnobj. ToString (); } }}
Then we can invoke it in the following way:
DateTime dt = DateTime.Now; var value = dt. Tonullstring ();
If, in the course of our conversion, we return a default value if we do not succeed, we can write the following code:
Public Static intToInt ( This ObjectObjintDefaultValue) { inti =DefaultValue; if(int. TryParse (obj. Tonullstring (), outi)) {returni; } Else { returnDefaultValue; } } Public StaticDateTime ToDateTime ( This Objectobj, DateTime defaultvalue) {DateTime I=DefaultValue; if(Datetime.tryparse (obj. Tonullstring (), outi)) {returni; } Else { returnDefaultValue; } }
Similarly, for custom classes, you can also extend the method as follows, if we create a new class city
//------------------------------------------------------------------------------//<auto-generated>//This code is generated from a template.////Manual changes to this file could cause unexpected behavior in your application.//Manual changes to this file would be overwritten if the code is regenerated.//</auto-generated>//------------------------------------------------------------------------------namespaceAVON. Dms. model{usingSystem; usingSystem.Collections.Generic; Public Partial classCity { Public decimalCityid {Get;Set; } Publicnullable<decimal> Provinceid {Get;Set; } Public stringZIP {Get;Set; } }}
We create a static method in the static class, as follows:
Public Static string Getzip ( This city,string defaultvalue) { string value = DefaultValue; if NULL ) { return City . ZIP; } Else return value; }
Thus, each time you create a new class city, you can call the extension method getzip to get the zip value of the current class.
Above, is my understanding of the C # extension class, if there is a wrong place, please correct me. Thank you.
Write here today, hope to help you, O (∩_∩) o haha ~
How to extend C # 3.0 Features