c#this关键字的四种用法总结

149 篇文章 148 订阅

1、代表当前类的对象

  class Father
    {
        public int Age { get; set; }
        public string Name { get; set; }
        public Father(int age, string name)
        {
            this.Age = age;
            this.Name = name;
        }
        public void Test()
        {
            Console.WriteLine($"name:{this.Name },age:{this.Age }");
        }

    }

调用:

  Father  father = new Father(100, "小明");
            father.Test ();
            father = new Father(50, "小李");
            father.Test();

输出:

name:小明,age:100
name:小李,age:50

2、在同一个类中在一个构造函数中调用另一个构造函数

 class Father
    {
        public int Age { get; set; }
        public string Name { get; set; }
        public int Height { get; set; }
        public Father(int age, string name)
        {
            this.Age = age;
            this.Name = name;
        }


      
        public void Test()
        {
            Console.WriteLine($"name:{this.Name },age:{this.Age }");
        }

    }

如果要传入三个参数,那么可以多加一个构造函数,如下:

  class Father
    {
        public int Age { get; set; }
        public string Name { get; set; }
        public int Height { get; set; }
        public Father(int age, string name)
        {
            this.Age = age;
            this.Name = name;
        }


        public Father(int age, string name, int height)
        {
            this.Age = age;
            this.Name = name;
            this.Height = height;
        }

        public void Test()
        {
            Console.WriteLine($"name:{this.Name },age:{this.Age }");
        }

    }

也可以使用this来简化代码,如下:

 class Father
    {
        public int Age { get; set; }
        public string Name { get; set; }
        public int Height { get; set; }
        public Father(int age, string name)
        {
            this.Age = age;
            this.Name = name;
        }


        public Father(int age, string name, int height):this (age ,name)
        {
            this.Height = height;
        }

        public void Test()
        {
            Console.WriteLine($"name:{this.Name },age:{this.Age }");
        }

    }

3、为某个类添加扩展方法
this后面指定为某个类添加扩展方法(扩展方法必须是静态方法,扩展方法所在的类也必须是静态类)

   class Father
    {
        public int Age { get; set; }
        public string Name { get; set; }
        public int Height { get; set; }
        public Father(int age, string name)
        {
            this.Age = age;
            this.Name = name;
        }


        public Father(int age, string name, int height) : this(age, name)
        {
            this.Height = height;
        }

        public void Test()
        {
            Console.WriteLine($"name:{this.Name },age:{this.Age }");
        }

    }
static class  FatherExtension
        {

        public static  void Eat(this Father father )//指定为Father类添加扩展方法,无参方法
        {
            Console.WriteLine("吃饭");
        }
        public static void Run(this Father father,bool isRun)//添加有参方法
        {
            Console.WriteLine($"{isRun }");
        }
    }

调用:

 Father father = new Father(100, "小明");
            father.Eat();
            father.Run(true );

输出:

吃饭
True

这里要注意的是:我们什么时候需要使用扩展方法,个人认为有两种情况,
1)某个类(非静态类)的源代码我们看不到,但是又想为这个类添加方法,这时候可以使用扩展方法,比如系统自带的一些类
2)为接口添加一个方法,并且这个方法对于实现接口的这些类来说方法都是一样的,如果在接口中添加这个方法的定义,那么我们就需要除了在接口中增加方法的定义以外,还需要在实现接口的每个类中写一个一模一样的方法,比如有个IPerson接口,里面有个MyAge的方法,然后我们现在想添加一个MyName的方法,并且MyName的内容一模一样,如果不使用扩展方法的话,则如下:
添加MyName方法前:

 interface IPerson
    {
        void MyAge();
      
    }


    class Chinese : IPerson
    {
        public void MyAge()
        {
            Console.WriteLine(10);
        }

    }


    class American : IPerson
    {
        public void MyAge()
        {
            Console.WriteLine(5);
        }

    }

添加MyName方法后:

  interface IPerson
    {
        void MyAge();
        void MyName();
    }


    class Chinese : IPerson
    {
        public void MyAge()
        {
            Console.WriteLine(10);
        }
        public  void MyName()
        {
            Console.WriteLine("小明");
        }
    }


    class American : IPerson
    {
        public void MyAge()
        {
            Console.WriteLine(5);
        }
        public  void MyName()
        {
            Console.WriteLine("小明");
        }
    }

可以看到在Chinese 类中、American 类中都添加了一个MyName的方法,如果接口的实现类有100个的话,那么我们还要添加100个一模一样的方法,这样无疑是重复的工作,如果通过通过扩展方法对接口进行扩展,则如下面的代码所示:

 interface IPerson
    {
        void MyAge();
      
    }

    class Chinese : IPerson
    {
        public void MyAge()
        {
            Console.WriteLine(10);
        }
    }


    class American : IPerson
    {
        public void MyAge()
        {
            Console.WriteLine(5);
        }
    }
    
   static  class IpersonExtension
    {

        public static void MyName(this IPerson person )
        {
            Console.WriteLine("小明");
        }

    }

从上面的代码可知,我们可以不更改接口和接口的实现类的代码,然后另外添加一个静态类,然后添加静态方法对接口进行扩展,同样实现了功能,但是减少了很多重复代码

4、索引器中用于访问集合元素
索引器类似属性。

 public  enum NameOrder
    {
        First,
        Second
    }

    public class Name<T>
    {


        public Name(T firstName, T lastName)
        {
            this.FirstName = firstName;
            this.LastName = LastName;
        }
        public T FirstName { get; set; }
        public T LastName { get; set; }
        public T this[NameOrder index]
        {
            get
            {
                switch (index)
                {
                    case NameOrder.First:
                        return FirstName;
                    case NameOrder.Second:
                        return LastName;
                    default:
                        return default;
                }
            }
        }
    }

调用:

Name<string> name = new Name<string>("四", "李");
            string firstName = name[NameOrder.First];
            Console.WriteLine(firstName );

输出

总结那么在c#看到this如何快速判断this属于哪一种用方法呢?可以这样识别:
1)this和字符’.'连接在一起,肯定是作为指代当前类对象使用
2)this是在冒号后面,那么是作为构造器中构成构造器链使用
3)this后面跟了一个类或者接口,那么是作为扩展方法使用
4)this后面跟了中括号[],那么就是作为索引器使用

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

c#上位机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值