C#扩展方法
2017-02-14 11:41
274 查看
本文转载自:http://www.cnblogs.com/suger/archive/2012/05/13/2498248.html
C#扩展方法的理解
“扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。”
这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些类型的基础上增加一个或多个方法,使用时不需要去修改或编译类型本身的代码。
先做个例子吧,以String为例,需要在字符串类型中加一个从字符串转为数值的功能。
以往我们可能是这样做的,会专门写一个方法做过转换
?
public static int StrToInt(string s)
{
int id;
int.TryParse(s, out id);//这里当转换失败时返回的id为0
return id;
}
调用就使用
?
string s = “abc”;
int i = StrToInt(s);
若是String类型有一个名为ToInt()(从字符串转为数值)的方法,就可以这样调用了
?
string s = “abc”;
int i = s.ToInt();
这样看起来是不是更好,下面来看看具体怎么实现吧
第一步:
我先创建一个解决方案,一个web应用程序(webtest)及一个类库(W.Common)
在webtest项目添加引用W.Common项目
第二步:在类库中新建一个名为EString.cs类
?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace W.Common
{
public static class EString
{
///
/// 将字符串转换为Int
///
///
/// 当转换失败时返回0
public static int ToInt(this string t)
{
int id;
int.TryParse(t, out id);//这里当转换失败时返回的id为0
return id;
}
}
}
看了上面的代码了吧,扩展方法规定类必须是一个静态类,EString是一个静态类,里面包含的所有方法都必须是静态方法。
msdn是这样规定扩展方法的:“扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。”
EString里有一个ToInt的静态方法,他接收一个自身参数this,类型为string,this string必须在方法参数的第一个位置。
这句话什么意思,即你需要对string扩展一个ToInt方法,this是string实例化后的对象,这可能说的不太清楚,我的表述能力能弱,不要见怪呀。。。通俗的说就是,扩展方法跟静态类的名称无关,只需要在一个静态类里面定义一个静态方法,第一个参数必须this string开头。
如果需要你要对DateTime类型扩展方法名为IsRange(判断是否在此时间范围内),代码如下:
?
///
/// 此时间是否在此范围内 -1:小于开始时间 0:在开始与结束时间范围内 1:已超出结束时间
///
///
///
///
///
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;
}
这里的扩展方法是用this DateTime打头,那么就可以这样调用
?
time.IsRange(t1,t2);//判断时间time是否在t1到t2的范围内
当前代码在使用扩展方法前需要先引用命名空间
?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using W.Common;//这里引用扩展方法所在的命名空间
namespace webtest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
use1();
Response.Write(“
”);
use2();
}
///
/// 没有用扩展方法
///
private void use1()
{
string s = “abc”;
int i = StrToInt(s);
Response.Write(“没有用扩展方法:” + i);
}
///
/// 使用扩展方法
///
private void use2()
{
string s = “2012”;
int i = s.ToInt();
Response.Write(“使用扩展方法:” + i);
}
public static int StrToInt(string s)
{
int id;
int.TryParse(s, out id);//这里当转换失败时返回的id为0
return id;
}
}
}
C#扩展方法的理解
“扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。”
这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些类型的基础上增加一个或多个方法,使用时不需要去修改或编译类型本身的代码。
先做个例子吧,以String为例,需要在字符串类型中加一个从字符串转为数值的功能。
以往我们可能是这样做的,会专门写一个方法做过转换
?
public static int StrToInt(string s)
{
int id;
int.TryParse(s, out id);//这里当转换失败时返回的id为0
return id;
}
调用就使用
?
string s = “abc”;
int i = StrToInt(s);
若是String类型有一个名为ToInt()(从字符串转为数值)的方法,就可以这样调用了
?
string s = “abc”;
int i = s.ToInt();
这样看起来是不是更好,下面来看看具体怎么实现吧
第一步:
我先创建一个解决方案,一个web应用程序(webtest)及一个类库(W.Common)
在webtest项目添加引用W.Common项目
第二步:在类库中新建一个名为EString.cs类
?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace W.Common
{
public static class EString
{
///
/// 将字符串转换为Int
///
///
/// 当转换失败时返回0
public static int ToInt(this string t)
{
int id;
int.TryParse(t, out id);//这里当转换失败时返回的id为0
return id;
}
}
}
看了上面的代码了吧,扩展方法规定类必须是一个静态类,EString是一个静态类,里面包含的所有方法都必须是静态方法。
msdn是这样规定扩展方法的:“扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。”
EString里有一个ToInt的静态方法,他接收一个自身参数this,类型为string,this string必须在方法参数的第一个位置。
这句话什么意思,即你需要对string扩展一个ToInt方法,this是string实例化后的对象,这可能说的不太清楚,我的表述能力能弱,不要见怪呀。。。通俗的说就是,扩展方法跟静态类的名称无关,只需要在一个静态类里面定义一个静态方法,第一个参数必须this string开头。
如果需要你要对DateTime类型扩展方法名为IsRange(判断是否在此时间范围内),代码如下:
?
///
/// 此时间是否在此范围内 -1:小于开始时间 0:在开始与结束时间范围内 1:已超出结束时间
///
///
///
///
///
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;
}
这里的扩展方法是用this DateTime打头,那么就可以这样调用
?
time.IsRange(t1,t2);//判断时间time是否在t1到t2的范围内
当前代码在使用扩展方法前需要先引用命名空间
?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using W.Common;//这里引用扩展方法所在的命名空间
namespace webtest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
use1();
Response.Write(“
”);
use2();
}
///
/// 没有用扩展方法
///
private void use1()
{
string s = “abc”;
int i = StrToInt(s);
Response.Write(“没有用扩展方法:” + i);
}
///
/// 使用扩展方法
///
private void use2()
{
string s = “2012”;
int i = s.ToInt();
Response.Write(“使用扩展方法:” + i);
}
public static int StrToInt(string s)
{
int id;
int.TryParse(s, out id);//这里当转换失败时返回的id为0
return id;
}
}
}
相关文章推荐
- C# 3.0新特性初步研究 Part2:使用扩展方法
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- Linq的那些事——从Linq扩展方法回顾C#语言基础
- c#中的扩展方法
- 向C#的String类添加按字节截取字符串的扩展方法
- C# 3.0语言新特性(语言规范):2 扩展方法
- 从C# 3.0说以人为本(三)—— 扩展方法
- C#2008新特性--(扩展方法)Extension Method
- C#3.5语言新特性(四)-扩展方法
- 实例 C#3.0与C#2.0相比之新特性(二)---扩展方法
- C# 中的扩展方法---Extension methods in C#
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- C# 3.0新特性初步研究 Part2:使用扩展方法_C#教程
- 不能不说的C#特性-扩展方法
- C# 3.0 扩展方法 实践
- 初探C# 3.0 (1) - 隐式类型变量, 扩展方法, Lambda表达式
- (二)初学C# 3.0之扩展方法
- C# 3.0 扩展方法 实践
- 向C#的String类添加按字节截取字符串的扩展方法
- 扩展方法(C# 3.0 语言)