The "this" keyword
is a special type of reference variable, that is implicitly defined within each
constructor and non-static method as a first parameter of the type class in
which it is defined, so The
"this" keyword refers to the class which you are currently writing
code in it. I mainly use it to distinct between method parameters and class
fields.
public class Person
{
string name =
""; //Field "name" in class Person
//Constructor
of the Person class, takes the name of the Person
//as argument
public Person
(string name)
{
//Assign
the value of the constructor argument "name" to the field
"name"
this.name
= name;
//If you'd miss out the "this"
here (name = name;) you would just assign the
//constructor argument to itself and the field "name" of the
//Person
class would keep its value "".
}
}
|