如题所述
在C#里this关键字主要有三种意思也分别对应各自的用途,见下:
1)在类的内部使用this是指“当前对象的”;
例如:
public class Foo{
public String Name { get; set; }
public virtual String MyFunction(){
return this.Name; // 返回当前对象的Name属性值
}
}
2)在构造函数圆括号尾部使用this是指“调用其它的构造函数”;
例如:
public class Foo{
public int Capacity { get; set; }
public Foo():this(4) // 此处的this指的是调用带参数的构造函数
{}
public Foo(int capacity){
this.Capacity = capacity;
}
}
3)在使用索引器时用this关键字作为修饰符。
例如:
public class Foo{
private ArrayList al = new ArrayList(2 << 4);
public virtual Object this[int index] // 索引器
{
get;
set;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-10-13
你可以就认为是全局的,比如你声明一个变量,方法里的参数生命了一个一样的变量,需要赋值时,可以用this来指明那个是全局变量,不声明的就是函数方法的局部变量
第2个回答 2007-12-10
this顾名思义,也就是当前的对象,用this可以访问该对象中的一系列属性,方法(除了静态的),这是面向对象编辑最基本的一个思想哦