I notice that if you have a private member in a class, you can access it in the class methods by just referring to it\'s name. You do not need to say this.memberName
"This" is almost always optional when you call members of current class.
However, it is used for other purposes, e.g. to pass the current class instance as a parameter or to set a property,field or variable to the current instance.
The only case where you must use it to call a method is when you call an extension method:
class AClass {
void CallIt() {
Test(); //not valid
this.Test(); //valid
}
}
static class AnExtension {
public static void Test(this AClass source) {
}
}
Yes, 'this' is implied. It can sometimes help for clarity. And it is also necessary for calling methods and need to reference the current class.
Yes, it's optional. The only times you'd have to use it are when you have a local variable that hides a member variable, or you want to refer to an indexed property (aka indexer).
You can optionally use this
in instance member access from within an instance members like an instance method or property because whenever an instance method is called this
(referring to the current object) is automatically passed in as an invisible parameter.
You cannot use this
from within static members to access instance member... like you cannot use this.x
or this.y
(or even simple x and y) from within a static method or property if x and y are instance members. This is because this
is undefined in a static member call. The static member belongs to the whole class... it has no idea which instance this
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 ClassName.MethodName();
So the static method does not know what object this
will refer.
this
is also not optional (it must be used) as the first modifier in the parameter list of an extension method. In fact this
is what identifies a static method as an extension method. Here now this
identifies the first parameter as the instance on which the extension method is working.
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);
}
}