Re: Why does a client code need to be recompiled also?

Tech-Archive recommends: Speed Up your PC by fixing your registry



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
.



Relevant Pages

  • RE: exec sp_help_job user account rights
    ... Execute permissions default to the public role in the msdb database. ... who can execute this procedure and is a member of the sysadmin fixed role can ... the SQL Server service is running. ... impersonation and sp_help_job is always executed under the security context ...
    (microsoft.public.sqlserver.security)
  • Re: SQL 2000 Windows Authentication - Same User Multiple Groups
    ... UpdateResearch stored procs. ... Resarch group (of which he is also a member), ... It's unclear to me why you explicitly denied execute on UpdateResearch to ... view-level permissions such that we can permit/deny a database action ...
    (microsoft.public.sqlserver.security)
  • Re: C# Equivalent to: For i = 30 To 32
    ... The initializer - declares and initializes the loop control variable ... Execute the initializer ... modify the loop control variable according to the modifier expression. ... exit to the code following the controlled block ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do I get the current date without Excels VBA!Now()?
    ... OK, so maybe Excel is not the problem, but when I execute this on an AS 2000 ... server with Excel installed, on good old Foodmart: ... WITH MEMBER Measures.VBAYear AS VBA!TRIM))) ... I am using Analysis Services 2005 without any service packs. ...
    (microsoft.public.sqlserver.olap)
  • Re: C# Equivalent to: For i = 30 To 32
    ... The initializer - declares and initializes the loop control variable ... Execute the initializer ... modify the loop control variable according to the modifier expression. ... exit to the code following the controlled block ...
    (microsoft.public.dotnet.languages.csharp)