class Program
{
static void Main(string[] args)
{
//扩展方法,在不修改原来的类代码上面添加新功能
//注意扩展方法的调用方式,和实例方法调用一致
object obj = new object();
Console.WriteLine(obj.Add(1, 2));
Shao s = new Shao("Y");
Console.WriteLine(s.Add(1, 2));
Console.ReadKey();
}
}
class Shao
{
private string Y = null;
public Shao(string kk)
{
Y = kk;
}
}
//扩展方法,必须是静态类和对应的静态方法
static class ExtenObject
{
public static int Add(this object obj,int a,int b)
{
return a + b;
}
}
//扩展方法
static class ExtenShao
{
public static int Add(this Shao obj, int a, int b)
{
return a + b;
}
}