您的位置:首页 > 编程语言 > C#

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

2016-04-16 22:32 525 查看

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; }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: