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);
}
}
posted @ 2010-07-18 20:32  深山老林  阅读(469)  评论(0编辑  收藏  举报