The extension method enables you to "add" methods to an existing type without creating a new derived type, re-compiling, or modifying the original type in other ways ."
This is what msdn says, that is, you can add one or more methods to these types, such as String, Int, DataRow, and able, you do not need to modify or compile the code of the type.
Let's take the String as an example. We need to add a function for converting from String to numeric in the String type.
We may have done this in the past, but we will write a method for conversion.
Public static int StrToInt (string s)
{
Int id;
Int. TryParse (s, out id); // The id returned when the conversion fails.
Return id;
}
Call
String s = "abc ";
Int I = StrToInt (s );
If the String type has a method named ToInt () (converted from String to numeric value), you can call it like this.
String s = "abc ";
Int I = s. ToInt ();
This looks better. Let's take a look at how to implement it.
Step 1:
First, I will create a solution, a web application (webtest) and a class library (W. Common)
Add and reference the W. Common Project in the webtest Project
Step 2: create a new class named EString. cs in the class library.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace W. Common
{
Public static class EString
{
/// <Summary>
/// Convert string to Int
/// </Summary>
/// <Param name = "t"> </param>
/// <Returns> If the conversion fails, 0 is returned. </returns>
Public static int ToInt (this string t)
{
Int id;
Int. TryParse (t, out id); // The id returned when the conversion fails.
Return id;
}
}
}
After reading the code above, the extension method requires that the class must be a static class, And the EString is a static class. All the methods contained in the class must be static methods.
Msdn specifies the extension method as follows: "extension methods are defined as static methods, but they are called through the instance method syntax. Their first parameter specifies the type of the method to act on, and the parameter is prefixed with the this modifier ."
In EString, there is a static ToInt method. It receives its own parameter this, which belongs to the string type. this string must be at the first position of the method parameter.
What do you mean by this sentence? You need to extend the ToInt Method to the string. this is the object after the string is instantiated. this may not be clear, and my presentation capabilities are weak, don't be surprised... In general, the extension method has nothing to do with the name of the static class. You only need to define a static method in a static class. The first parameter must start with this string.
If you need to name the DateTime Type Extension Method IsRange (to determine whether it is within this time range), the Code is as follows:
/// <Summary>
/// Whether the time is in this range-1: less than the start time 0: within the range of start and end time 1: The End Time has exceeded
/// </Summary>
/// <Param name = "t"> </param>
/// <Param name = "startTime"> </param>
/// <Param name = "endTime"> </param>
/// <Returns> </returns>
Public static int IsRange (this DateTime t, DateTime startTime, DateTime endTime)
{
If (startTime-t). TotalSeconds> 0 ))
{
Return-1;
}
If (endTime-t). TotalSeconds <0 ))
{
Return 1;
}
Return 0;
}
The extension method here is to use this DateTime as the header, so you can call it like this
Time. IsRange (t1, t2); // determines whether the time is within the range of t1 to t2.
Before using the extension method, you must reference the namespace www.2cto.com.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using W. Common; // reference the namespace of the extension method.
Namespace webtest
{
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Use1 ();
Response. Write ("<br/> ");
Use2 ();
}
/// <Summary>
/// No extension method used
/// </Summary>
Private void use1 ()
{
String s = "abc ";
Int I = StrToInt (s );
Response. Write ("extension method not used:" + I );
}
/// <Summary>
/// Use the Extension Method
/// </Summary>
Private void use2 ()
{
String s = "2012 ";
Int I = s. ToInt ();
Response. Write ("using Extension Method:" + I );
}
Public static int StrToInt (string s)
{
Int id;
Int. TryParse (s, out id); // The id returned when the conversion fails.
Return id;
}
}
}
The above is my understanding and use of the extension method. please correct me if there are any errors or deficiencies. Thank you ..
This is the first time I wrote an article. It took a long time. I used to read other people's articles. Now I know that it is really not easy to write a good article.
Study hard and stick to your dreams.
From suger