Jon Skeet said:
I think we'll probably have to disagree on this. I think there are just
so many differences between properties and fields (space and garbage
collection implications, exceptions, time taken, limitations such as
ref/out etc) that thinking of them as fields leads to far more
confusion than thinking of them as syntactic sugar over method calls.
Whereas I consider the field like qualities(Associated get and set, field
like syntax, etc) suggests that they shouldn't be considered as just
syntactic sugar for methods(as a note, I generally consider it bad form to
use a field as a ref or an out, I'd rather use a variable and assign the
field post-method). As Frans said in another post in this
thread(paraphrased), a field is a field, a method a method, and a property a
property. I think its all a matter of where each of us thinks the concept
lies. I doubt any of our idea of what properties are\should be are really
taht different, they just happen to lie on opposite sides of the "ideal
property" line.
I don't regard the phrase "syntactic sugar" as derogatory like some
people do, btw. The using(...) statement is syntactic sugar, but
incredibly useful - just like properties. That said, I think it's
important to recognise syntactic sugar for what it is, and know what
lies behind it.
I agree. Knowing what the underlying implementation does is vital for anyone
writing a major piece of code. I would argue, at times, that its less
important to know how things work underneath if you are just writing a
script or other minor thing. I just don't always agree with the distinction
of syntactic sugar vs primary langauge feature. Much of a HLL is syntactic
sugar. You don't really need a good number of constructs to write proper
programs(for loops, switches, using(in all of its forms), lock, default
event accessors, else), but they certainly make life easier. Taking it far
enough you don't even need arithmetic operators to perform work properly, if
you are willing to dive to an assembly language, it still makes life
considerably easier to use them.
Beyond that, most people probably still don't understand how properties
really work. They know that they compile to 2 methods(In C# and VB atleast,
thats not really a restriction) prefixed with get_ and set_(although that
isn't strictly required either), but fewer seem to realize that a property
itself has no accessibility or even that a property is a seperate metadata
construct which associates those methods(in other words, the methods
themselves do not make up the property, the property references the
methods).
Personally, given the runtime support for properties, to be fully correct I
would probably be hesitant to call properties syntactic sugar, although the
compiler does offer sugary syntax for defining them, the concept is baked
into the runtime as properties. The compiler would have to provide something
to distinguish them, perhaps like the __property keyword in C++.