muler wrote:
Hi all,
Can someone please elaborate on (or annotate) this text excerpt I found
while reading "The C# Programming Language", by Anders Hejlsberg, Scott
Wiltamuth, Peter Golde:
Not read it, so there might be context I'm missing, but I'll have a go.
>
Section 7.1
A property access:
Every property access has an associated type, namely the type of the
property. Furthermore, a property access may have an associated
instance expression. {up to this it is clear}.
OK.
When an accessor (the
get or set block) of an instance property access is invoked, the result
of evaluating the instance expression becomes the instance represented
by this. {this last statement is not clear - my understanding is: the
result is a value with type the type of the property.}
I think what it means is: In the code for a property accessor (whether
get or set), the keyword 'this' refers to that particular instance whose
property is being accessed.
Is the final 'this' in a different font in the original text, perhaps
the font used for code?
Example (air code):
class Foo
{
int _halfbar;
public int Bar
{
get { return this._halfbar * 2; }
set { this._halfbar = value / 2; }
}
public static void Main(string[] args)
{
Foo foo1 = new Foo();
Foo foo2 = new Foo();
foo1.Bar = 6;
foo2.Bar = 3;
Console.WriteLine("foo2.Bar is {0}", foo2.Bar.ToString());
return;
}
}
Here, the 'this' in the code for Foo.Bar refers successively to the
instance referred to by foo1, the instance referred to by foo2, and the
instance referred to by foo1.
Without knowing the structure of the book, I couldn't say how plausible
this relatively simple explanation is for this section of the book.
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version