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#
スコープ (クラスまたはメソッド) 内で同じ名前の 2 つ以上の変数を宣言することはできません。ただし、インスタンス変数とパラメーターは同じ名前を持つ場合があります。たとえば、
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
を使用してこの問題を解決できます .
例:これと同じ名前の変数
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
を使用できます キーワード。たとえば、
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);
ここで、this
を超えました passParameter() に 方法。 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[] プライベートです。そのため、プログラムからアクセスすることはできません クラス。
ここで、配列の値にアクセスして設定するには、インデクサーを使用します。
Student s1 = new Student();
s1[0] = "Ram";
s1[1] = "Shyam";
s1[2] = "Gopal";
this
を使用したので インデクサーを作成するには、Student のオブジェクトを使用する必要があります クラスでインデクサーにアクセスします。 インデクサーの詳細については、C# インデクサーにアクセスしてください。
C言語