Please Help: Expression Classifications

M

muler

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:

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}. 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.}

Thanks in advance.
 
L

Larry Lard

muler said:
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.
 
J

Jon Shemitz

muler said:
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}. 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.}

Larry is perfectly correct - the last sentence is just saying that the
"instance expression" acts like any other method of the class, and
`this. Whatever` refers to the Whatever instance member. (My copy (2e)
has a reference to 7.5.7, "this Access."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top