首页 > Windows开发 > 详细

c# this关键字用法

时间:2020-02-13 14:39:29      阅读:68      评论:0      收藏:0      [点我收藏+]
  • 用法1 为原始类型扩展方法

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

/// <summary>
    /// 扩展类 用于为原始类扩展方法  
    /// </summary>
   public static class AM_Extends
    {
        /// <summary>
        /// 为string类扩展了一个child方法,实现某功能
        /// </summary>
        /// <param name="str"></param>
        /// <param name="new_str"></param>
        public static void Child( this string str,string new_str)
        {
            object obj = str;
            str=new_str;
        }
    }

 

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);//st3的值为“123”
7         }

 

 

 

 

 

 

 

 

 

c# this关键字用法

原文:https://www.cnblogs.com/MatureMan/p/12303347.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!