İsteğe bağlı olarak, bir örnek yöntemi ya da özelliği gibi bir örneği üyeleri arasından, örneğin elemanı erişim this
kullanabilir. x ve y örnek üyeler ise statik yöntem veya özellik içinden this.x
veya this.y
(hatta basit x ve y) kullanamazsınız gibi
Sen ... örnek üyesine erişmek için statik üyeleri içinden this
kullanamaz. Bunun nedeni, statik üye çağrısında this
undefined olmasıdır. Statik üye tüm sınıfa aittir ... this
'un hangi örneğe başvurduğunu bilmez. Ve bunun nedeni, statik bir yöntem veya özellik çağırdığınızda, aramanın ClassName.MethodName();
biçiminde olması nedeniyle, statik yöntem, this
öğesinin hangi nesneye başvurduğunu bilmemesinden kaynaklanır.
this
bir uzantısı yönteminin parametre listesindeki ilk modifiye edici olarak (bu kullanılmalıdır) aynı zamanda isteğe bağlı değildir. Aslında this
, bir uzatma yöntemi olarak statik bir metodu tanımlar. Şimdi burada this
, uzantı yönteminin çalıştığı örnek olarak ilk parametreyi tanımlar.
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);
}
}
Ben yarısı diğer bir düzine altı olduğunu düşünüyorum: bir uzantısı yöntem çağırdığınızda
Bir yöntemi çağırmak için kullanabilirsiniz gereken tek vaka
olduğunu. – R0MANARMYEvet, isteğe bağlıdır. Benim düşünceme göre, "bu" nın aşırı kullanımı kodu karıştırıyor. –
[C# Kullanılacak Ne Zaman Kullanılır? Bu "Anahtar Kelime" nin olası kopyası [http://stackoverflow.com/questions/843288/c-when-to-use-this-keyword) –