在 C# 中,this
关键字引用类的当前实例。例如,
using System;
namespace ThisKeyword {
class Test {
int num;
Test(int num) {
// this.num refers to the instance field
this.num = num;
Console.WriteLine("object of this: " + this);
}
static void Main(string[] args) {
Test t1 = new Test(4);
Console.WriteLine("object of t1: " + t1);
Console.ReadLine();
}
}
}
输出
object of this: ThisKeyword.Test object of t1: ThisKeyword.Test
在上面的示例中,我们创建了一个名为 t1 的 Test 类对象。我们打印了对象 t1 的名称和类的 this
关键字。
在这里,我们可以看到 t1 和 this
的名称是相同的。这是因为 this
关键字引用了类的当前实例,也就是 t1。
以下是 this
关键字在 C# 中的一些主要用途。
C# this 关键字与同名变量
我们不能在同一个作用域(类或方法)内声明两个或多个同名变量。但是,实例变量和参数可以具有相同的名称。例如,
using System;
namespace ThisKeyword {
class Test {
int num;
Test(int num) {
num = num;
}
static void Main(string[] args) {
Test t1 = new Test(4);
Console.WriteLine("value of num: " + t1.num);
Console.ReadLine();
}
}
}
输出
0
在上面的程序中,实例变量和参数的名称相同:num。我们已将 4 作为值传递给构造函数。
但是,我们得到的输出是 **0**。这是因为 C# 感到困惑,因为实例变量和参数的名称相同。
我们可以通过使用 this
来解决这个问题。
示例:this 关键字与同名变量
using System;
namespace ThisKeyword {
class Test {
int num;
Test(int num) {
// this.num refers to the instance field
this.num = num;
}
static void Main(string[] args) {
Test t1 = new Test(4);
Console.WriteLine("value of num: " +t1.num);
Console.ReadLine();
}
}
}
输出
value of num: 4
现在,我们得到了预期的输出,即 **4**。这是因为 this.num
引用了类的实例变量。
因此,实例变量和参数的名称之间没有混淆。
使用 this 调用同一类的构造函数
在处理构造函数重载时,我们可能需要从一个构造函数调用另一个构造函数。在这种情况下,我们可以使用 this
关键字。例如,
using System;
namespace ThisKeyword {
class Test {
Test(int num1, int num2) {
Console.WriteLine("Constructor with two parameter");
}
// invokes the constructor with 2 parameters
Test(int num) : this(33, 22) {
Console.WriteLine("Constructor with one parameter");
}
public static void Main(String[] args) {
Test t1 = new Test(11);
Console.ReadLine();
}
}
}
输出
Constructor with two parameter Constructor with one parameter
在上面的示例中,我们使用 :
加上 this
关键字从构造函数 Test(int num)
调用构造函数 Test(int num1, num2)
。
当我们调用 Test(int num)
构造函数时,Test(int num1, int num2)
构造函数会先执行。
注意:从一个构造函数调用另一个构造函数称为构造函数链。
C# this 作为对象参数
我们可以使用 this
关键字将当前对象作为参数传递给方法。例如,
using System;
namespace ThisKeyword {
class Test {
int num1;
int num2;
Test() {
num1 = 22;
num2 = 33;
}
// method that accepts this as argument
void passParameter(Test t1) {
Console.WriteLine("num1: " + num1);
Console.WriteLine("num2: " + num2);
}
void display() {
// passing this as a parameter
passParameter(this);
}
public static void Main(String[] args) {
Test t1 = new Test();
t1.display();
Console.ReadLine();
}
}
}
输出
num1: 22 num2: 33
在上面的程序中,我们有一个方法 passParameter()。它接受类的对象作为参数。
passParameter(this);
这里,我们将 this
传递给了 passParameter() 方法。由于 this
引用类的实例,我们可以访问 num1 和 num2 的值。
this 声明 C# 索引器
索引器允许类的对象像数组一样被索引。我们使用 this 关键字在 C# 中声明索引器。例如,
using System;
namespace ThisKeyword {
class Student {
private string[] name = new string[3];
// declaring an indexer
public string this[int index] {
// returns value of array element
get {
return name[index];
}
// sets value of array element
set {
name[index] = value;
}
}
}
class Program {
public static void Main() {
Student s1 = new Student();
s1[0] = "Ram";
s1[1] = "Shyam";
s1[2] = "Gopal";
for (int i = 0; i < 3; i++) {
Console.WriteLine(s1[i] + " ");
}
}
}
}
输出
Ram Shyam Gopal
在上面的程序中,我们使用 this
关键字创建了一个索引器。
数组 name[] 是私有的。因此,我们无法从 Program 类访问它。
现在,要访问和设置数组的值,我们使用索引器。
Student s1 = new Student();
s1[0] = "Ram";
s1[1] = "Shyam";
s1[2] = "Gopal";
由于我们使用 this
创建了索引器,因此我们必须使用 Student 类的对象来访问索引器。要了解有关索引器的更多信息,请访问 C# 索引器。