Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can optionally use <code>this</code> in instance member access from within an instance members like an instance method or property because whenever an instance method is called <code>this</code> (referring to the current object) is automatically passed in as an invisible parameter. </p> <p>You cannot use <code>this</code> from within static members to access instance member... like you cannot use <code>this.x</code> or <code>this.y</code> (or even simple x and y) from within a static method or property if x and y are instance members. This is because <code>this</code> is undefined in a static member call. The static member belongs to the whole class... it has no idea which instance <code>this</code> is referring to. And that is due to the fact that when you call a static method or property , the call is of the format <code>ClassName.MethodName();</code> So the static method does not know what object <code>this</code> will refer. </p> <p><code>this</code> is also not optional (it must be used) as the first modifier in the parameter list of an extension method. In fact <code>this</code> is what identifies a static method as an extension method. Here now <code>this</code> identifies the first parameter as the instance on which the extension method is working. </p> <pre><code> 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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload