C#のthis拡張
587 ワード
C#ではthisを使用して既存のクラスにメソッドを追加し、呼び出すのが便利です.
一、例
stringに色を変更する
二、条件静的クラス 静的方法(方法の前にstaticを加える) 最初のパラメータの前にthis(すなわち自分を表す) を付ける
一、例
stringに色を変更する
public static class StringTool
{
public static string GreenColor(this string str)
{
return "" + str + " ";
}
public static string RedColor(this string str)
{
return "" + str + " ";
}
}
コールモードstring str = "hello world";
str = str.GreenColor();
こうして表示されるとstrは緑のマークをつけて緑になります.二、条件