跳到主要内容

C# this关键字

提示
  1. this关键字的含义:在C#中,this 关键字表示类的当前实例。例如,this.num 指向类实例中的 num 字段,用于区分实例变量和方法参数。
  2. this关键字的主要用途
    • 解决实例变量和参数名称冲突:当实例变量和方法参数同名时,使用 this 来区分。
    • 构造函数链:使用 this 调用同一类中的另一个构造函数。
    • 作为方法参数:使用 this 将当前对象作为参数传递给其他方法。
    • 声明索引器:使用 this 关键字声明类的索引器,允许类的对象像数组一样被索引。
  3. 在实例中this关键字的行为:在实例方法中,this 指向调用该方法的对象实例。例如,在构造函数中,this 指向正在创建的对象实例。

在 C# 中,this 关键字指的是类的当前实例。例如,

using System;

namespace ThisKeyword {
class Test {

int num;
Test(int num) {
// this.num 指的是实例字段
this.num = num;
Console.WriteLine("this 的对象: " + this);
}

static void Main(string[] args) {

Test t1 = new Test(4);
Console.WriteLine("t1 的对象: " + t1);
Console.ReadLine();
}
}
}

输出

this 的对象: ThisKeyword.Test
t1 的对象: ThisKeyword.Test

在上述示例中,我们创建了一个名为 t1Test 类的对象。我们打印了对象 t1 和类的 this 关键字的名称。

这里,我们可以看到 t1this 的名称是相同的。这是因为 this 关键字指的是类的当前实例,即 t1

以下是 C# 中 this 关键字的一些主要用途。

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("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 指的是实例字段
this.num = num;
}

static void Main(string[] args) {

Test t1 = new Test(4);
Console.WriteLine("num 的值: " +t1.num);
Console.ReadLine();
}
}
}

输出

num 的值: 4

现在,我们得到了预期的输出 4。这是因为 this.num 指的是类的实例变量。

因此,实例变量和参数的名称之间没有混淆。

使用 this 调用同一类的构造函数

在处理构造函数重载时,我们可能需要从一个构造函数调用另一个构造函数。在这种情况下,我们可以使用 this 关键字。例如,

using System;

namespace ThisKeyword {
class Test {

Test(int num1, int num2) {

Console.WriteLine("带有两个参数的构造函数");
}

// 调用带有 2 个参数的构造函数
Test(int num) : this(33, 22) {

Console.WriteLine("带有一个参数的构造函数");
}

public static void Main(String[] args) {

Test t1 = new Test(11);
Console.ReadLine();
}
}
}

输出

带有两个参数的构造函数
带有一个参数的构造函数

在上述示例中,我们使用了 : 后跟 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;
}

// 接受 this 作为参数的方法
void passParameter(Test t1) {
Console.WriteLine("num1: " + num1);
Console.WriteLine("num2: " + num2);
}

void display() {
// 将 this 作为参数传递
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 指的是类的实例,我们可以访问 num1num2 的值。

使用 this 声明 C# 索引器

索引器允许类的对象像数组一样被索引。我们使用 this 关键字在 C# 中声明索引器。例如,

using System;
namespace ThisKeyword {

class Student {

private string[] name = new string[3];

// 声明一个索引器
public string this[int index] {

// 返回数组元素的值
get {
return name[index];
}

// 设置数组元素的值
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# 索引器。