用法1 this代表当前类的实例对象 当我们定义了一个类的全局变量时 而该类方法中也声明了相同的参数名时 如何区分两个相同参数名称的调用 使用this可以更直观地看到this.参数名 为全局参数。

首先声明一个类

 public class TestThisClass
    {
        //用法一 this代表当前类的实例对象
        private string scope = "全局变量";
        public string getResult()
        {
            string scope = "局部变量";
            // this代表TestThisClass的实例对象
            // 所以this.scope对应的是全局变量
            // scope对应的是getResult方法内的局部变量
            return this.scope + "-" + scope;
        }
    }

 

我在mian函数中使用

   public static void Main(string[] args)
{

            //用法一 this代表当前类的实例对象
            TestThisClass testThisClass = new TestThisClass();
            Console.WriteLine(testThisClass.getResult());

}

 

输出结果 注意是先全局变量再局部变量

 

用法2 用this串联构造函数 (:this()方法) 目的是为了实例化该类时 还会先自动调用一次base()中对应参数的方法类 再继续执行原本的方法

首先声明一个类

    public class TsetThisCLClass
    {
        public TsetThisCLClass()
        {
            Console.WriteLine("无参构造函数");
        }
        // this()对应有两个参构造方法TsetThisCLClass(string text, string text2)
        // 先执行TsetThisCLClass(string text, string text2),后执行TsetThisCLClass(string text)
        public TsetThisCLClass(string text) : this("李四", text)
        {
            Console.WriteLine(text);
            Console.WriteLine("有参构造函数");
        }

        public TsetThisCLClass(string text, string text2)
        {
            Console.WriteLine(text + text2);
            Console.WriteLine("有两个参数的参构造函数");
        }
    }

 

我在mian函数中使用

         //用法二  用this串联构造函数  (:base()方法)
            TsetThisCLClass test = new TsetThisCLClass("张三");

 

输出结果 注意是先输出base中带两个参数的方法 再输出本身

 

用法3 为原始类型扩展方法 主要是用来我们平时经常用到的类型 (string,object)

首先声明一个扩展类

  public static class Extends
    {
        // string类型扩展ToJson方法
        public static object stringToJson(this string Json)
        {
            return Json == null ? null : JsonConvert.DeserializeObject(Json);
        }
        // object类型扩展ToJson方法
        public static string objectToJson(this object obj)
        {
            var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            return JsonConvert.SerializeObject(obj, timeConverter);
        }
    }

 

具体调用

            object a = "asjfh";
            string b = "afsdd";
            a.objectToJson(); //这里的a是object类型 他可直接调用扩展方法 this object方法中声明的内容 
            b.stringToJson(); //这里的b是string类型 他可直接调用扩展方法 this string方法中声明的内容

 

 

举个例子 .net core注入配置文件 使用this 方法

        public void ConfigureServices(IServiceCollection services)
        {
                var builder = new ConfigurationBuilder();
                builder.AddJsonFile($"{AppDomain.CurrentDomain.BaseDirectory}/A.json", false, true);
                var config = builder.Build();
                services.AddAlhgInfoConf(config);//调用下面的方法 
        }

 

再声明一个方法

         //这里的方法 声明了参数 this IServiceCollection services 所以上面的services可以直接调用AddAlhgInfoConf该方法 这是属于this的扩展方法
        public static IServiceCollection AddAlhgInfoConf(this IServiceCollection services, IConfiguration configuration)
        {
            services.Configure<AlhgInfoConf>(configuration);
            return services;
        }

 

 
来自:https://www.cnblogs.com/yunnn/p/15573062.html