在C#中访问成员时,`this`关键字是可选的吗?

我注意到如果你在一个类中有一个私有成员,你可以通过引用它的名称在类方法中访问它。 你不需要说this.memberName ,只是memberName有效。 那么在成员访问的上下文中this关键字是可选的吗?

当你想澄清范围时,我确实看到它很有用 – 当你有两个同名的变量时。 访问会员时是否还有其他理由使用它?

是的,它是可选的。 您必须使用它的唯一时间是当您有一个隐藏成员变量的局部变量,或者您想要引用索引属性(也称为索引器) 。

您可以选择在实例成员(例如实例方法或属性)中使用this实例成员访问权限,因为无论何时调用实例方法, this方法(引用当前对象)都会自动作为不可见参数传入。

你不能在静态成员中使用this来访问实例成员…就像你不能在静态方法或属性中使用this.xthis.y (甚至简单的x和y),如果x和y是实例成员。 这是因为在静态成员调用中未定义。 静态成员属于整个类……它不知道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 (ie 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级的成员时,“这个”几乎总是可选的。

但是,它用于其他目的,例如将当前类实例作为参数传递或将属性,字段或变量设置为当前实例。

您必须使用它来调用方法的唯一情况是您调用扩展方法时:

 class AClass { void CallIt() { Test(); //not valid this.Test(); //valid } } static class AnExtension { public static void Test(this AClass source) { } }