クラスにプライベート メンバーがある場合、その名前を参照するだけで、クラス メソッドでアクセスできることに気付きました。this.memberName
と言う必要はありませんmemberName
。メンバー アクセスのコンテキストでは、 this キーワードはオプションですか?
スコープを明確にしたい場合、つまり同じ名前の変数が 2 つある場合に便利です。メンバーにアクセスするときにそれを使用する他の理由はありますか?
はい、オプションです。これを使用する必要があるのは、メンバー変数を非表示にするローカル変数がある場合、またはインデックス付きプロパティ (別名インデクサー)を参照する場合のみです。
this
インスタンス メソッドやプロパティなどのインスタンス メンバー内からインスタンス メンバー アクセスを オプションで使用できます。これは、インスタンス メソッドが呼び出されるたびにthis
(現在のオブジェクトを参照する)、非表示のパラメーターとして自動的に渡されるためです。
x と y がインスタンス メンバーである場合、静的メソッドまたはプロパティ内からor (または単純な x と y でさえ) を使用できないthis
ように、静的メンバー内からインスタンス メンバーにアクセスすることはできません。これは、静的メンバー呼び出しで定義されていないためです。静的メンバーはクラス全体に属しています...どのインスタンスが参照されているかわかりません。これは、静的メソッドまたはプロパティを呼び出す場合、呼び出しの形式が であるという事実によるものです。そのため、静的メソッドは、どのオブジェクトが参照されるかを知りません。this.x
this.y
this
this
ClassName.MethodName();
this
this
また、拡張メソッドのパラメーター リストの最初の修飾子としてオプションではありません (使用する必要があります)。実際this
には、静的メソッドを拡張メソッドとして識別するものです。ここ this
で、拡張メソッドが機能しているインスタンスとして最初のパラメーターを識別します。
using System;
class Class_name
{
static string static_variable="static";
string instance_variable="instance";
static void Main()
{
Class_name object_name = new Class_name();
Console.WriteLine("Printing out instance and static variables from within Main() body :");
Console.WriteLine(object_name.instance_variable);
Console.WriteLine(Class_name.static_variable);
/* Note that we cannot say either of the following :
object_name.static_variable
Class_name.instance_variable
*/
Console.WriteLine();
// now lets call the static and instance methods
object_name.Instance_method(); // Now this is the key call which
// passes "this" as an invisible parameter
// to the Instance_method. "this" refers to
// object_name
Class_name.Static_method();// "this" is NOT passed to Static_method() because now
// the call is made on Class_name ... so there is nothing
// to be represented by "this"
Console.ReadLine();
}
void Instance_method()
{
// here we receive "this" as an invisible parameter referring
// to the object on which Instance_method is called (i.e. object_name)...
// ... see the Main() method for comments at the call site.
Console.Write("Instace method called ... " +
"prints out instance variable twice, with and without 'this': ");
// the following two calls mean exactly the same.
Console.Write(this.instance_variable);
Console.WriteLine (instance_variable);
// one little additional point is that static members are
// accessible from within instance members
Console.WriteLine();
Console.Write("static variables can also be accessed from within Instance_method: ");
Console.WriteLine(static_variable);
Console.WriteLine();
Console.WriteLine();
}
static void Static_method()
{
// See the Main() method body for the call Class_name.Static_method()
// Notice that this method is called on Class_name and not object_name
// which means that there is no invisibly passed-in "this" parameter available
// in this method.
// we can also not access the instance_variable in this method
// as instance variables are always part of some object. This method
// is not called on any object, its called on Class_name.
// Console.WriteLine(instance_variable); // Compiler error
Console.WriteLine("Static method called ... prints out static variable: ");
Console.WriteLine(static_variable);
}
}
現在のクラスのメンバーを呼び出す場合、「これ」はほとんどの場合オプションです。
ただし、現在のクラス インスタンスをパラメーターとして渡したり、プロパティ、フィールド、または変数を現在のインスタンスに設定するなど、他の目的で使用されます。
メソッドを呼び出すためにこれを使用する必要がある唯一のケースは、拡張メソッドを呼び出す場合です。
class AClass {
void CallIt() {
Test(); //not valid
this.Test(); //valid
}
}
static class AnExtension {
public static void Test(this AClass source) {
}
}
はい、「これ」が暗示されています。明確にするのに役立つ場合があります。また、メソッドを呼び出すためにも必要であり、現在のクラスを参照する必要があります。