Dear All,
I am a bit new to C#. I am a bit confused about the "this" keyword. The way they define "this" keyword is that it is the reference to the current object of the class
but want to know how to use it . Can some 1 show me some very simple examples.
I have found 1 example on the msdn.
public Employee(string name,string alias)
{
this.name=name;
this.alias=alias;
}
Can some one explain me the "this" keyword in the above example . To which object it is refering to. What does "this" means over here. A few simple examples will be greatly appreciated.
thanks,
Sunil Sabir

Use of "this" keyword
Izu
Let's start with a simple class that has a single private member variable and a constructor that initializes that variable...
class MyClass
{
private string value;
public MyClass( string initialValue)
{
value = initialValue;
}
}
No, let's look at another example with a slight difference...
class MyClass
{
private string initialValue;
public MyClass( string initialValue)
{
initialValue = initialValue;
}
}
OOPS! That doesn't do what we want it too! The member variable will not get initialized, all we did was set the parameter equal to itself! This is where the "this" keyword comes in handy. Adding "this" tells the compiler that we're talking about the variable that belongs to MyClass, not the parameter. Let's try it again using the "this" keyword...
class MyClass
{
private string initialValue;
public MyClass( string initialValue)
{
this.initialValue = initialValue;
}
}
And we get what we wanted.
jlawing
this.close
The action is "close"
The keyword "this" refers to the object (in this case the form) that you are performing the action on. If the object you're performing the action on happens to be the CURRENT object, then you can use "this" instead of the long name of that object.
ElizabethS
In the example above, the class declaration is not shown but you infer from the example that the class has at least two string members: one with the identifier "name" and one with the identifier "alias", like so:
class UnknownClassName
{
string name;
string alias;
public Employee(string name, string alias)
{
this.name = name;
this.alias = alias;
}
}
The 'this' keyword is used in the Employee method to distinguish between the instance variables of the class and the parameters being passed in. In this case, since the parameters and instance identifiers are identical, the 'this' keyword is required to access the instance variables. Had the identifiers been different, the 'this' keyword wouldn't be required:
class UnknownClassName
{
string strName;
string strAlias;
public Employee(string name, string alias)
{
strName = name;
strAlias = alias;
}
}
Note that static methods by definition do not have an instance associated with them so the 'this' keyword is not available:
class X
{
static string strStatic;
static void Foo()
{
// static methods operate without an instance so
// the 'this' keyword would cause an error. However,
// static data of the class is available.
strStatic = "bar";
}
}
If I created an instance:
UnknownClassName x = new UnknownClassName();
and called the Employee member:
x.Employee("me", "wasMe");
the 'this' keyword in the Employee method would refer to the x instance of the caller.
- Ray