我注意到如果你在一个类中有一个私有成员,你可以通过引用它的名称在类方法中访问它。您无需说this.memberName
,只需memberName
即可。那么在成员访问的上下文中this关键字是可选的吗?
当你想澄清范围时,我确实看到它很有用 - 当你有两个同名的变量时。访问会员时是否还有其他理由使用它?
答案 0 :(得分:12)
是的,它是可选的。您必须使用它的唯一时间是当您有一个隐藏成员变量的局部变量,或者您想要引用indexed property (aka indexer)时。
答案 1 :(得分:1)
您可以选择在实例成员(例如实例方法或属性)中使用this
实例成员访问,因为无论何时调用实例方法this
(引用当前对象)都会自动传入作为一个不可见的参数。
您无法在静态成员中使用this
来访问实例成员...就像您无法在静态方法中使用this.x
或this.y
(甚至是简单的x和y)或x或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);
}
}
答案 2 :(得分:0)
是的,'这个'是隐含的。它有时可以帮助澄清。而且调用方法也需要引用当前的类。
答案 3 :(得分:0)
当你调用当前类的成员时,“this”几乎总是可选的。
但是,它用于其他目的,例如将当前类实例作为参数传递或将属性,字段或变量设置为当前实例。
您必须使用它来调用方法的唯一情况是您调用扩展方法时:
class AClass {
void CallIt() {
Test(); //not valid
this.Test(); //valid
}
}
static class AnExtension {
public static void Test(this AClass source) {
}
}