c#3.0提供的扩展方法

在c#3.0之前,想要为内置的类型添加一个方法显然是不可能的。但是,c#3.0提供的扩展方法可以解决这个问题。具体代码如下:

    public static class ExtendedClass
{
public static string ToKevin(this string str)
{
return "hello,kevin";
}
}
public class Program
{
static void Main(string[] args)
{
DateTime dt = DateTime.Now;
string str1 = dt.ToShortDateString().ToKevin();
Console.WriteLine(str1);
}
}
Untitled Page .net技术交流三号群:196882675
作者:深山老林
出处:http://wlb.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted @ 2010-07-18 20:32 深山老林 阅读(231) 评论(0) 编辑 收藏

公告

英文名:Kevin

位置:中国 北京

职位:技术总监

Email:iamwlb@qq.com

昵称:深山老林
园龄:4年1个月
粉丝:92
关注:4