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, 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.
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, 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.
No comments:
Post a Comment