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

掌握 C# this 关键字:消歧义、构造函数链、传递实例与索引器

掌握 C# this 关键字

通过简明解释与实战代码样本,探索 C# this 关键字的多种用法。

在 C# 中,this 指向当前类实例。它在区分实例成员与方法参数、调用重载构造函数、将当前对象传递给其他方法、声明索引器等场景中不可或缺。

using System;
namespace ThisKeyword {
    class Test {
        int num;
        Test(int num) {
            // this.num 指向实例字段
            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 和 this 指向同一实例,故字符串表示相同。


消除同名变量歧义

实例字段与参数可同名,但无 this 时编译器视为参数,字段保持默认值。

using System;
namespace ThisKeyword {
    class Test {
        int num;
        Test(int num) {
            num = num; // 参数自赋值,字段保持默认 0
        }
        static void Main(string[] args) {
            Test t1 = new Test(4);
            Console.WriteLine("value of num: " + t1.num);
            Console.ReadLine();
        }
    }
}

输出

0

输出为 0,因赋值未影响字段。用 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("value of num: " + t1.num);
            Console.ReadLine();
        }
    }
}

输出

value of num: 4

构造函数链调用

类提供多个构造函数时,可用 this 调用另一个,集中初始化逻辑。在构造函数签名后用冒号加 this 实现。

using System;
namespace ThisKeyword {
    class Test {
        Test(int num1, int num2) {
            Console.WriteLine("Constructor with two 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 parameters
Constructor with one parameter

单参数构造函数先执行双参数构造函数,再执行自身体。这种构造函数链模式保持初始化逻辑 DRY。


将当前实例传递给方法

有时方法需操作调用它的对象。用 this 作为参数使意图显式。

using System;
namespace ThisKeyword {
    class Test {
        int num1;
        int num2;
        Test() {
            num1 = 22;
            num2 = 33;
        }
        // 接受 Test 实例的方法
        void PassParameter(Test t) {
            Console.WriteLine("num1: " + t.num1);
            Console.WriteLine("num2: " + t.num2);
        }
        void Display() {
            // 传递当前实例
            PassParameter(this);
        }
        public static void Main(string[] args) {
            Test t1 = new Test();
            t1.Display();
            Console.ReadLine();
        }
    }
}

输出

num1: 22
num2: 33

因 this 代表调用实例,方法可安全访问其私有字段。


声明索引器

索引器使对象可像数组一样索引。this 关键字声明索引器签名。

using System;
namespace ThisKeyword {
    class Student {
        private string[] names = new string[3];
        // 索引器声明
        public string this[int index] {
            get { return names[index]; }
            set { names[index] = value; }
        }
    }
    class Program {
        public static void Main() {
            Student s = new Student();
            s[0] = "Ram";
            s[1] = "Shyam";
            s[2] = "Gopal";
            for (int i = 0; i < 3; i++) {
                Console.WriteLine(s[i]);
            }
        }
    }
}

输出

Ram
Shyam
Gopal

私有 names 数组通过索引器访问,保持封装的同时提供数组式语法。

深入了解索引器请参考微软官方文档:C# 索引器。

C语言

  1. 树莓派 DS18B20 温度传感器测试与 HC-SR04 超声波测距实战
  2. 飞针测试(FPT)专业指南:PCB 功能验证的自动化利器
  3. Turck TB3-CP80:感应/磁性/电容/光电传感器精准测试盒
  4. Pomona MP1 磁吸测试表笔 —— 4mm 安全插孔、直角设计,工业精准诊断利器
  5. 快速智能手机COVID-19检测 10分钟高精度出结果
  6. RTI 严格软件测试:确保 Connext DDS 跨平台可靠性
  7. 汽车测试未来:HIL系统与RTI DDS集成
  8. C# static 关键字:静态变量、方法与类完全掌握
  9. Coded UI 测试自动化框架:从入门到实战的完整指南
  10. 精通Python global关键字:用法、时机与最佳实践
  11. Java StringBuilder 与 StringBuffer:高性能字符串操作
  12. 深入理解 Java final 关键字:变量、方法与类的不可变性