1、扩展方法:类名前面加static ,方法参数前 加this,如:对string类进行扩展
public static class string
{
public static string Quert(this string s) //注意这里的参数形式
{
return "[" + s + "]";
}
}
使用的时候:
"aaa".Quert();//返回"[aaa]"
2、仿StringBuilder的IntBuilder,实现的功能:可以像StringBuilder一样加
public class IntBuilder
{
int sum=0;
public IntBuilder Append(int n)
{
sum+=n;
return this;
}
public int ToInt()
{
return sum;
}
}
使用:
IntBuilder ib=new IntBuilder();
ib.Append(1).Append(1).Append(3).Append(2);
ib.ToInt();//得到的结果为7