c# this关键字用法

  • 用法1 为原始类型扩展方法

先说一下,this 后面跟的类型,就是要拓展方法的类型。注意要写在静态类中的静态方法,不然有些情况下访问不到。

 1 /// <summary>
 2     /// 扩展类 用于为原始类扩展方法  
 3     /// </summary>
 4    public static class AM_Extends
 5     {
 6         /// <summary>
 7         /// 为string类扩展了一个child方法,实现某功能
 8         /// </summary>
 9         /// <param name="str"></param>
10         /// <param name="new_str"></param>
11         public static void Child( this string str,string new_str)
12         {
13             object obj = str;
14             str=new_str;
15         }
16     }
定义扩展方法
1  private void Form1_Load(object sender, EventArgs e)
2         {
3             string st1 = "123";
4             string st2 = "";
5             string st3 = "";
6             st3 = st2.Child(st1);
7 
8         }
调用实例

猜你喜欢

转载自www.cnblogs.com/MatureMan/p/12303347.html