È possibile utilizzare this
in un accesso membro di esempio da un membro di un'istanza come un metodo di istanza o una proprietà perché ogni volta che un metodo di istanza viene chiamato this
(facendo riferimento all'oggetto corrente) viene automaticamente passato come un parametro invisibile.
Non è possibile utilizzare this
dall'interno membri statici per accedere membro di istanza ... come non è possibile utilizzare this.x
o this.y
(o anche semplici x ed y) da all'interno di un metodo statico o proprietà se x ed y sono membri di istanza. Questo perché this
non è definito in una chiamata membro statica. Il membro statico appartiene all'intera classe ... non ha idea a quale istanza si riferisce this
. E questo è dovuto al fatto che quando si chiama un metodo o una proprietà statica, la chiamata è del formato ClassName.MethodName();
Quindi il metodo statico non sa quale oggetto si riferirà a this
.
this
non è facoltativo (deve essere utilizzato) come primo modificatore nell'elenco dei parametri di un metodo di estensione. Infatti, this
è ciò che identifica un metodo statico come metodo di estensione. Qui ora this
identifica il primo parametro come istanza su cui funziona il metodo di estensione.
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);
}
}
Se il compilatore non si lamenta Penso che sia sei di una mezza dozzina dell'altro. – R0MANARMY
Sì, è opzionale. A mio parere, l'uso eccessivo di "questo" ingombra il codice. –
possibile duplicato di [C# Quando utilizzare "questa" parola chiave] (http://stackoverflow.com/questions/843288/c-when-to-use-this-keyword) –