C#基础知识之this关键字
时间:2016-04-16 22:55:56
收藏:0
阅读:292
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; }
}
}
原文:http://www.cnblogs.com/liujie2272/p/5399603.html
评论(0)