C#基础知识二之this关键字

this关键字

  • 引用类的当前实例,包括继承而来的方法,通常可以省略。
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public Person(string Name, int Age)
            {
                this.Age = Age;
                this.Name = Name;
            }
        }
  •  将对象作为参数传递到其他方法。
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public Person(string Name, int Age)
            {
                this.Age = Age;
                this.Name = Name;
            }
            public void CallTest(Person person)
            {
                Console.WriteLine(person.Name + person.Age);
            }
            public void Call()
            {
                CallTest(this);
            }
        }
  • 声明索引器
        public class Person
        {
            string[] PersonList = new string[10];
            public string this[int param]
            {
                get { return PersonList[param]; }
                set { PersonList[param] = value; }
            }
        }
原文地址:https://www.cnblogs.com/liujie2272/p/5399603.html