亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C#这个关键字

C# 这个关键字

在本文中,我们将通过示例了解 C# 中的这个关键字。

在 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 num1, num2) 来自构造函数 Test(int num) .

当我们调用 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);

在这里,我们通过了 thispassParameter() 方法。作为 this 引用类的实例,我们可以访问 num1 的值 和 num2 .


这个来声明一个 C# 索引器

索引器允许类的对象像数组一样被索引。我们使用这个关键字在 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# indexer。


C语言

  1. RTI的软件测试
  2. 汽车行业的未来
  3. C# 静态关键字
  4. Python 全局关键字
  5. Java this 关键字
  6. Java final 关键字
  7. 测试 DS18B20 传感器
  8. 编码的 UI 测试自动化框架教程
  9. 图尔克TB3-CP80传感器测试箱
  10. Pomona 磁性测试探针 MP1
  11. 基于智能手机的 COVID-19 测试
  12. 飞针测试 (FPT):了解这种 PCB 测试技术