Reserved word this is only used in constructors, class methods, and instances of classes, and it has the following meanings:
This appears in the constructor of the class as a value type that represents a reference to the object itself being constructed.
This appears as a value type in the method of the class, which represents a reference to the object that called the method.
This appears as a variable type in the structure's constructor, which represents a reference to the structure being constructed.
This appears as a variable type in the method of the structure, which represents a reference to the structure that invokes the method.
In addition, it is illegal to use this reserved word elsewhere.
The following code shows how to use this reserved word.
Program Listing 10-2:
Using System;
Class A
{public
int x;
public void Main ()
{
x=5;
Console.WriteLine ("The value of x is" {0} ", x);
Console.WriteLine ("The Value of This.x is:{0}", this.x);
}
The result of the program running should be:
The value of x is:5
The value of This.x is:5
Note: If you are a C + + programmer, use C # to forget the this pointer in C + + and forget symbols such as this-> and::. As long as you have the concept of nested names, you use "." Is enough.
Here is an example to illustrate the use of this.
Program Listing 10-3:
Using System;
Class Fact
{
int x;
public int getfact ()
{
float temp;
int save=x;
int a=1;
while (X>a)
{
a++;
temp=this.x/a;
X/=a;
if ((float) x!=temp) {
return-1
}
}
Swap (this.x,save);
return save
}
}
The factorial that the program uses to find whether an integer is another integer. If so, the class fact method is Getfact () returns the integer, otherwise, getfact () returns-1.
In fact, within C #, this is defined as a constant. Therefore, it is illegal to use statements such as this++,this--. However, this can be used as a return value.
We know that in the Windows operating system, the current window problem is highlighted and we call the window activated. For example, in Microsoft Word, we can open multiple documents at the same time. Each document window acts as a child of the main Microsoft Word window, where only one child window is the currently active window. If Microsoft Word does not have any documents open, the main window acts as the currently active window.
In the following example, we declare a window class windows, assuming that Windows can open up to five child windows at the same time. Windows Method GetActiveWindow () is used to return the currently active window. If a child window is open, an instance of the child window is returned, otherwise the main window itself is returned.
Program Listing 10-4:
Using System;
Class window
{public
window[] m_childwindow=new window[5];//child window public
bool Ishavechild=false;//have child windows Public
bool IsActive;//window is activated public
window GetActiveWindow ()
{
if (ishavechild==false) {
Isactive=true;
return this; Returns the window itself
}
else{for
(int i=0;i<5;i++) {
if (m_childwindow[i). isactive==true{return
m_childwindow[i];
Returns the activated child window
}
}