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);
}
}
原文地址:https://www.cnblogs.com/wwwzzg168/p/3571985.html