欢迎进入IT起步网社区,中国最大的IT新手技术交流平台>>进入
扩展方法定义如下: http://www.mscto.com wwwxrend.com bbs.xrend.com
public static class Extensions{
public static void Foo(this string s) {
…
}
}
使用:
String s=“Hello,World”;
s.Foo();
IT起步社区 bbs.xrend.com public static void Foo(this string s) {
…
}
}
使用:
String s=“Hello,World”;
s.Foo();
我们以前会对一些参数、对象进行某些处理,而写一些独立方法来封装这些处理语句;这些方法为了方便重用,可能会写在一些公共类里供大家调用,例如: IT起步网 IT问吧
IT起步网 www.xrend.com
IT起步社区
定义一个公共方法计算每天有多少分钟,
public class PublicCenter
{
//传入参数为天数
public long GetDaysMin(int pDays)
{
return pDays * 24 * 60; IT起步网
}
}
外部使用调用:
int days = 3;
PublicCenter pCenter = new PublicCenter();
int minutes = pCenter.GetDaysMin(days);
public class PublicCenter
{
//传入参数为天数
public long GetDaysMin(int pDays)
{
return pDays * 24 * 60; IT起步网
}
}
外部使用调用:
int days = 3;
PublicCenter pCenter = new PublicCenter();
int minutes = pCenter.GetDaysMin(days);
我想大家对上面这段代码的使用都非常熟悉,以前这样处理的情况非常的多!现在我们看看用扩展方法来处理会是怎么样;
www.xrend.com
www.xrend.com
IT blog wordpress.xrend.com
定义一个静态类
0
最新评论共有 0 位网友发表了评论
查看所有评论
发表评论



