The this Keyword


Before concluding this chapter, it is necessary to introduce this. When a method is called, it is automatically passed an implicit argument that is a reference to the invoking object (that is, the object on which the method is called). This reference is called this. To understand this, first consider a program that creates a class called Rect that encapsulates the width and height of a rectangle and that includes a method called area( ) that returns its area:

 using System; class Rect {   public int width;   public int height;   public Rect(int w, int h) {     width = w;     height = h;   }   public int area() {     return width * height;   } } class UseRect {   public static void Main() {     Rect r1 = new Rect(4, 5);     Rect r2 = new Rect(7, 9);     Console.WriteLine("Area of r1: " + r1.area());     Console.WriteLine("Area of r2: " + r2.area());   } }

As you know, within a method, the other members of a class can be accessed directly, without any object or class qualification. Thus, inside area( ), the statement

 return width * height;

means that the copies of width and height associated with the invoking object will be multiplied together and the result returned. However, the same statement can also be written like this:

 return this.width * this.height;

Here, this refers to the object on which area( ) was called. Thus, this.width refers to that object’s copy of width, and this.height refers to that object’s copy of height. For example, if area( ) had been invoked on an object called x, then this in the preceding statement would have been referring to x. Writing the statement without using this is really just shorthand.

Here is the entire Rect class written using the this reference:

 using System; class Rect {   public int width;   public int height;   public Rect(int w, int h) {     this.width = w;     this.height = h;   }   public int area() {     return this.width * this.height;   } } class UseRect {   public static void Main() {     Rect r1 = new Rect(4, 5);     Rect r2 = new Rect(7, 9);     Console.WriteLine("Area of r1: " + r1.area());     Console.WriteLine("Area of r2: " + r2.area());   } }

Actually, no C# programmer would use this as just shown, because nothing is gained, and the standard form is easier. However, this has some important uses. For example, the C# syntax permits the name of a parameter or a local variable to be the same as the name of an instance variable. When this happens, the local name hides the instance variable. You can gain access to the hidden instance variable by referring to it through this. For example, while not recommended style, the following is a syntactically valid way to write the Rect( ) constructor:

 public Rect(int width, int height) {   this.width = width;   this.height = height; }

In this version, the names of the parameters are the same as the names of the instance variables, thus hiding them. However, this is used to “uncover” the instance variables.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net