C# this关键字使用及扩展方法

反射、自定义特性这篇文章最有为解决字符串类型转换,使用了扩张方法。
详细了解this关键字(参考 微软官网-this(C# 参考))和扩展方法(参考 微软官网-扩展方法(C# 编程指南)

This 关键字

代指类的当前实例

1、限定类似名称隐藏的成员

当类里面的全局字段 与 某个方法内的临时变量重名时,若再想取全局字段需加上this关键字获取。
我个人的自定义编码规范是,全局变量为m_+小写字母开头的驼峰。临时变量为_开头。

新建TestClass

using System.Diagnostics;

namespace LchCommon
{
   
    public class TestClass
    {
   

        //=================== construct function ==================	
        #region construct 构造
        /// <summary>
        /// 构造
        /// </summary>
        public TestClass()
        {
   

        }
        #endregion



        //=================== private fields ======================
        #region fields
        private string m_data = "全局字段";
        private string m_data1 = "全局字段1";
        #endregion


        //=================== public metod ========================
        #region TestMethod 测试方法
        /// <summary>
        /// 测试方法
        /// </summary>
        public void TestMethod()
        {
   
            string m_data = "方法内变量";
            int m_data1 = 0;
            this.m_data1 = "全局字段修改2";

            Debug.WriteLine(m_data);
            Debug.WriteLine(m_data1);
            Debug
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值