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