现在的位置: 首页 > 综合 > 正文

c#3.0提供的扩展方法

2013年01月10日 ⁄ 综合 ⁄ 共 298字 ⁄ 字号 评论关闭

在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);
}
}

抱歉!评论已关闭.