工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
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

出力がゼロなのは、代入がフィールドに影響しないためです。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 を使ったコンストラクターチェーン

クラスが複数のコンストラクターを提供する場合、初期化ロジックを集約するために 1 つが別のコンストラクターを呼び出せます。これはコンストラクターシグネチャのコロンに続けて this キーワードで実現します。

using System;
namespace ThisKeyword {
    class Test {
        Test(int num1, int num2) {
            Console.WriteLine("Constructor with two parameters");
        }
        // 2 パラメータコンストラクターを呼び出し
        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

1 パラメータコンストラクターが最初に 2 パラメータコンストラクターを実行し、その後自身の本体を続行します。このパターンはコンストラクターチェーニングと呼ばれ、初期化ロジックを 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 を使ったインデクサー宣言

インデクサーによりオブジェクトを配列のようにインデックスアクセスできます。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 配列がインデクサー経由でアクセスされ、カプセル化を保ちながら配列ライクな構文を提供します。

インデクサーの詳細については、Microsoft の公式ドキュメントを参照してください:C# インデクサー。

C言語

  1. Raspberry PiでDS18B20温度センサーをテスト
  2. フライングプローブテスト(FPT)完全ガイド:信頼性の高いPCB機能検証
  3. Turck TB3-CP80:誘導・磁気・静電容量・光電センサー対応 精密センサーテストボックス
  4. Pomona MP1 磁気テストプローブ:4mm安全ジャック、右角度で精密産業診断
  5. スマートフォンベースCOVID-19迅速検査、10分で高精度な結果を提供
  6. RTIにおける厳格なソフトウェアテスト:Connext DDS全体での信頼性確保
  7. C# static キーワード:静的変数、メソッド、クラスをマスターする
  8. Coded UI Test自動化フレームワーク:初心者向け包括ガイド
  9. Python globalキーワード完全マスター: 使い方とタイミング
  10. Javaのthisキーワード徹底解説:実践的な用法とコンストラクタ活用
  11. Javaのfinalキーワード完全解説:変数、メソッド、クラスでの活用法