扩展方法
//必须定义在独立 静态类中
// 方法参数类型前加this 方法参数类型就是要扩展的类型
public static class strHelper
{
public static bool IsCon(this string x)
{
if (string.IsNullOrEmpty(x))
return false;
var xxx = x[0];
//转大写
return char.IsUpper(x[0]);
}
}
//像原始方法那样调用就行 nice~
"test".IsCon();
////////////////////////接口也能一样扩展
///////////////////////扩展方法链
public static string a(this string x)
{
if (string.IsNullOrEmpty(x))
return ""+x;
return "王蛋" + x;
}
public static string b(this string x)
{
if (string.IsNullOrEmpty(x))
return ""+x;
return "地蛋" + x;
}
//链式调用 顺序执行
Console.WriteLine("test".a().b());

浙公网安备 33010602011771号