Re: Why does a client code need to be recompiled also?
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Wed, 07 Jan 2009 23:40:34 -0800
On Wed, 07 Jan 2009 14:29:43 -0800, <beginwithl@xxxxxxxxx> wrote:
hi
1) From the book:
From what book?
Illustrated C#
Thanks. I don't happen to have a copy, but knowing what book you're talking about may help others provide answers, if they do have their own copy.
[...]As you probably are aware, this works fine. And you're right, technically
speaking, the value of "b", being a reference to an instance of B, cannot
be known until run-time. So the exact language the book uses is incorrect.
Could you make an educated guess about what the author was most likely
trying to convey?
The author may in fact not be familiar with the rules. It wouldn't be the first time someone published a programming book written by someone who has some gaps in their knowledge.
As far as what might have led him to write such a thing, even if his intended point was wrong, there are in fact restrictions on what you can put in a type's field initializer. In particular, you cannot do things that would be undefined at the moment that the initializer is executed. The big no-no would be referring to any instance members, since the initializer is executed before _all_ of the constructors, so there's no safe way to ensure that an instance method would execute correctly.
The C# specification, difficult to read but the only definitive reference, has all the specifics if you're more curious. My feeling, without having a copy of the book in hand, is that the author simply didn't double-check his facts and did in fact intend to convey the point you inferred, wrong though it was.
[...]The syntax is identical, yes. But what's generated by the compiler is
completely different. In the case of the field, the compiled code simply
dereferences the object and accesses the field. In the case of the
property, the compiled code calls the "get" method of the property to
retrieve the value.
So if you change from a field to a property, you _must_ recompile the
client code so that it performs the appropriate action, even though the
syntax is the same.
This is a bit off topic, but why couldn’t the assembly containing
class X also contain code that generates a call to “get” method of a
property A? Wouldn’t in that case the encapsulation be even more
effective, since now we could freely replace a field with a property
( assuming they both have the same name and type), without the need
for the client code to be recompiled?
I'm not sure what you mean. The assembly containing class X is where the field/property is declared, not where it's used (in your example). You could put all sorts of things in that assembly, but it's not going to affect how the client executes.
If you're asking why C# doesn't just wrap field and property access with a single utility method that either interacts with a field or a property depending on how the member is declared, I'd say the main reason for doing that is performance. However, there is also the issue that fields and property don't actually exactly the same semantics; one particular example is that you can have a write-only property, but not a write-only field. So, there's not any feasible way to create a single common method that handles access for a member both a field and a property.
Pete
.
- References:
- Why does a client code need to be recompiled also?
- From: beginwithl
- Re: Why does a client code need to be recompiled also?
- From: Peter Duniho
- Re: Why does a client code need to be recompiled also?
- From: Michael Starberg
- Re: Why does a client code need to be recompiled also?
- From: Jeff Johnson
- Re: Why does a client code need to be recompiled also?
- From: beginwithl
- Why does a client code need to be recompiled also?
- Prev by Date: Re: Discovery: dictionaries load slow unless you have the right key/value pair in the right format
- Next by Date: Re: const objects on a function level?
- Previous by thread: Re: Why does a client code need to be recompiled also?
- Next by thread: Re: Why does a client code need to be recompiled also?
- Index(es):
Relevant Pages
|