When to use `this' in the code?
- To avoid the nameing scopes, e.g.
public class Rectangle:Shape{ public Rectangle(double Width, double Height):base(){ this.Width = Width; this.Height = Height; } }
If you have the parameters named as Width and Height, you have to use `this' to avoid ambiguity. Otherwise, you may neglect it as...
public class Rectangle:Shape{ public Rectangle(double w, double h):base(){ Width = w; Height = h; } }
-
To refer to the current instance, e.g.
and the implementation of the interface should refer to `this' as well...public interface IIndex<out T>{ T this[int i]{get;} int Count{get;} }
public Rectangle this[int i]{ get{ if(i<0||i>=data.Length){ throw new ArgumentOutOfRangeException("index"); } return data[i]; } }