this 关键字将引用类的当前实例。静态成员函数没有 this 指针。this 关键字可用于从构造函数、实例方法和实例访问器中访问成员。以下是 this 的常用用途: 限定被相似的名称隐藏的成员,例如: public Employee(string name, string alias) { this.name = name; this.alias = alias; } 将对象作为参数传递到其他方法,例如: CalcTax(this); 声明索引器,例如: public int this [int param] { get { return array[param]; } set { array[param] = value; } } 在静态方法、静态属性访问器或字段声明的变量初始值设定项中引用 this 是错误的。
this是当前对象,如窗体、按钮等等,如果不指明则表示当前类。
示例 在本例中,this 用于限定 Employee 类成员 name 和 alias,它们都被相似的名称隐藏。this 还用于将对象传递到属于其他类的方法 CalcTax。// keywords_this.cs // this example using System; public class Employee { public string name; public string alias; public decimal salary = 3000.00m; // Constructor: public Employee(string name, string alias) { // Use this to qualify the fields, name and alias: this.name = name; this.alias = alias; } // Printing method: public void printEmployee() { Console.WriteLine("Name: {0}\nAlias: {1}", name, alias); // Passing the object to the CalcTax method by using this: Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this)); } } public class Tax { public static decimal CalcTax(Employee E) { return (0.08m*(E.salary)); } }public class MainClass { public static void Main() { // Create objects: Employee E1 = new Employee ("John M. Trainer", "jtrainer"); // Display results: E1.printEmployee(); } } 输出 Name: John M. Trainer Alias: jtrainer Taxes: $240.00
this就是指当前的应用程序本身,在非静态方法、静态属性等中可以使用。
this有几个作用:一、解决名称冲突:public class ClassA { int id;/*A*/ public ClassA ( int id/*B*/ ) { this.id/*A*/ = id/*B*/; } }二、返回当前类的实例。public class ClassA { //省略。 ClassA Add( object o ) { //执行操作 return this; } }然后,就可以在代码中这样调用: ObjectA.Add( x ).Add( y ).Add( z ); 三、调用显示实现接口或者被隐藏的基类的方法public class ClassA : ClassB, IInterfaceA { public MethodA() { ((ClassB) this).MethodH(); ((IInterfaceA) this).MethodH(); } void IInterfaceA.MethodH() { //省略 } public new void MethodH() { //省略 } } 上面所说的只是this的一些常见用法,关键是要理解this是什么,至于this的用法,则是千变万化。上面所列出的三个例子,则是必须要用this才能实现的一些常见的问题。
public Employee(string name, string alias)
{
this.name = name;
this.alias = alias;
}
将对象作为参数传递到其他方法,例如:
CalcTax(this);
声明索引器,例如:
public int this [int param]
{
get
{
return array[param];
}
set
{
array[param] = value;
}
}
在静态方法、静态属性访问器或字段声明的变量初始值设定项中引用 this 是错误的。
在本例中,this 用于限定 Employee 类成员 name 和 alias,它们都被相似的名称隐藏。this 还用于将对象传递到属于其他类的方法 CalcTax。// keywords_this.cs
// this example
using System;
public class Employee
{
public string name;
public string alias;
public decimal salary = 3000.00m; // Constructor:
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
} // Printing method:
public void printEmployee()
{
Console.WriteLine("Name: {0}\nAlias: {1}", name, alias);
// Passing the object to the CalcTax method by using this:
Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this));
}
}
public class Tax
{
public static decimal CalcTax(Employee E)
{
return (0.08m*(E.salary));
}
}public class MainClass
{
public static void Main()
{
// Create objects:
Employee E1 = new Employee ("John M. Trainer", "jtrainer"); // Display results:
E1.printEmployee();
}
}
输出
Name: John M. Trainer
Alias: jtrainer
Taxes: $240.00
{
int id;/*A*/ public ClassA ( int id/*B*/ )
{
this.id/*A*/ = id/*B*/;
}
}二、返回当前类的实例。public class ClassA
{
//省略。 ClassA Add( object o )
{
//执行操作 return this;
}
}然后,就可以在代码中这样调用:
ObjectA.Add( x ).Add( y ).Add( z );
三、调用显示实现接口或者被隐藏的基类的方法public class ClassA : ClassB, IInterfaceA
{ public MethodA()
{
((ClassB) this).MethodH(); ((IInterfaceA) this).MethodH();
} void IInterfaceA.MethodH()
{
//省略
} public new void MethodH()
{
//省略
}
}
上面所说的只是this的一些常见用法,关键是要理解this是什么,至于this的用法,则是千变万化。上面所列出的三个例子,则是必须要用this才能实现的一些常见的问题。