473,385 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is C# 2.0 syntax of Properties closed?

I red MSDN article of C# 2.0 this week... and i found
very strange syntax for properties e.g.:

public int MyIntValue {
get {
// ...
}
protected set {
// ...
}
}

.... Is this syntax defined and obligatory?

Maybe it's not only my feeling that aboved syntax is
not a best choice.
Any answer and discussion will be very interesting.

Marcin
Nov 16 '05 #1
23 2020
Hi Marcin,

This is just an example that you can have different access modifiers for
get and set. Get is public, set is private, and so on.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Morten Wennevik wrote:
Hi Marcin,

This is just an example that you can have different access modifiers for
get and set. Get is public, set is private, and so on.

Happy coding!


Thanx... but i don't like this syntax. Because it is
self-conflicting.
First i have set property access to public "public" and then i have to
set e.g. set access to "protected" or "private"...

I think that so much better from logical point-of-view looks:

int MyInt {
public get {
// ...
}
protected set {
// ...
}
}

// or even

public int MyInt {
get {...}
}

protected int MyInt {
set { ... }
}

Marcin
Nov 16 '05 #3

"Marcin GrzA?bski" <mg*******@taxussi.no.com.spam.pl> wrote in message
news:c4**********@nemesis.news.tpi.pl...
Morten Wennevik wrote:
Hi Marcin,

This is just an example that you can have different access modifiers for
get and set. Get is public, set is private, and so on.

Happy coding!
Thanx... but i don't like this syntax. Because it is
self-conflicting.
First i have set property access to public "public" and then i have to
set e.g. set access to "protected" or "private"...

I think that so much better from logical point-of-view looks:

int MyInt {
public get {
// ...
}
protected set {
// ...
}
}

// or even

public int MyInt {
get {...}
}

protected int MyInt {
set { ... }
}


Frankly, while I don't particularly care for the existing syntax, I don't
think either of these are any better. Seperating the accessors makes it less
immedaitly clear that they are related and putting the access modifiers on
the accessor makes it less immediatly clear that the property is public(as
far as most langauges are concerned at any rate, in metadata a property has
no accessibility).

Marcin

Nov 16 '05 #4
Hi Daniel,

int MyInt {
public get {
// ...
}
protected set {
// ...
}
}

// or even

public int MyInt {
get {...}
}

protected int MyInt {
set { ... }
}

Frankly, while I don't particularly care for the existing syntax, I don't
think either of these are any better. Seperating the accessors makes it less
immedaitly clear that they are related and putting the access modifiers on
the accessor makes it less immediatly clear that the property is public(as
far as most langauges are concerned at any rate, in metadata a property has
no accessibility).


What do You think about a sample code below...
(I don't know real C# 2.0 syntax rules and this sample is only
for speculate)

internal int MyInt {
get {
// ...
}
protected set {
// is this "protected internal" or "protected" ?
}
}

or

internal int MyInt {
public get {
// is this "internal" or "public" ?
}
set {
// ...
}
}

and what about setting "main" accessor to "protected" or
"private"?

I think that a properties are so close to a methods that it
was no reason to treat them other than a methods.

Marcin
Nov 16 '05 #5

"Marcin Grzebski" <mg*******@void.taxussi.com.pl.void> wrote in message
news:c4***********@mamut1.aster.pl...
Hi Daniel,

int MyInt {
public get {
// ...
}
protected set {
// ...
}
}

// or even

public int MyInt {
get {...}
}

protected int MyInt {
set { ... }
}

Frankly, while I don't particularly care for the existing syntax, I don't
think either of these are any better. Seperating the accessors makes it
less immedaitly clear that they are related and putting the access
modifiers on the accessor makes it less immediatly clear that the
property is public(as far as most langauges are concerned at any rate, in
metadata a property has no accessibility).


What do You think about a sample code below...
(I don't know real C# 2.0 syntax rules and this sample is only
for speculate)

internal int MyInt {
get {
// ...
}
protected set {
// is this "protected internal" or "protected" ?
}
}

or

internal int MyInt {
public get {
// is this "internal" or "public" ?
}
set {
// ...
}
}

and what about setting "main" accessor to "protected" or
"private"?


This is where things get tricky. If I was writing the complier I would issue
an error in both cases because you are providing an accessor at greater
accessibility than the property. IMHO, explicit accessor access modifiers
should only be able to reduce accessibility.

I think that a properties are so close to a methods that it
was no reason to treat them other than a methods.
In that case, then, I would say that properties should be done away with and
java like GetXXX and SetXXX should be used instead. A property *is not* a
method as far as the langauge is concerned. Its a field with accessors that
happen to be implemented as methods in the runtime.
Marcin

Nov 16 '05 #6
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
In that case, then, I would say that properties should be done away with and
java like GetXXX and SetXXX should be used instead. A property *is not* a
method as far as the langauge is concerned. Its a field with accessors that
happen to be implemented as methods in the runtime.


Here I disagree. It's far closer to a method call than a field, to my
mind. It has the syntax of a field, but the semantics of a method call,
which is why you can't use a property as a ref parameter, for instance.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Marcin Grzębski <mg*******@void.taxussi.com.pl.void> wrote in
news:c4***********@mamut1.aster.pl:
What do You think about a sample code below...
(I don't know real C# 2.0 syntax rules and this sample is only
for speculate)

internal int MyInt {
get {
// ...
}
protected set {
// is this "protected internal" or "protected" ?
}
}
This would beg for a separation of get and set clauses in a
construction I think. I then would opt for:
internal int MyInt get
{
// code
}

internal protected int MyInt set
{
}
or

internal int MyInt {
public get {
// is this "internal" or "public" ?
}
set {
// ...
}
}

and what about setting "main" accessor to "protected" or
"private"?
The two are combined: the property name and the get or set, you
can't see them separated. So specifying 2 accessor operators on different
places is not that correct.
I think that a properties are so close to a methods that it
was no reason to treat them other than a methods.


They're not close to methods, if they were they'd accept
parameters. (Ok, the indexer property accepts a parameter). So they can't
be threated as methods, more like fields, or better: virtual fields.

FB

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #8
Frans Bouma [C# MVP] <pe******************@xs4all.nl> wrote:
I think that a properties are so close to a methods that it
was no reason to treat them other than a methods.


They're not close to methods, if they were they'd accept
parameters. (Ok, the indexer property accepts a parameter). So they can't
be threated as methods, more like fields, or better: virtual fields.


I think they're much more like methods than parameters. Consider:

o They end up as methods internally
o They can be virtual, overridden etc
o They *can* take parameters, as indexers - although C# is quite
restrictive in terms of its indexers, compared with other .NET
languages.
o They can't be used as ref/out parameters

So, in what way are they like fields *apart* from in access syntax?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
"Jon Skeet [C# MVP]" wrote...
I think they're much more like methods than parameters. Consider:
[snip]
o They *can* take parameters, as indexers - although C#
is quite restrictive in terms of its indexers, compared
with other .NET languages.


You can even extend that argument, as *all* properties with a setter can
make use of the default argument: value.

So it definitely is closer to methods than fields.

Furthermore, even Microsoft obviously consider properties in terms of
methods. If you reverse engineer a class into UML with the combination
VS.NET/Visio, properties show up in the operation section of the class!

// Bjorn A


Nov 16 '05 #10
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP***********************@msnews.microsoft.co m:
Frans Bouma [C# MVP] <pe******************@xs4all.nl> wrote:
> I think that a properties are so close to a methods that it
> was no reason to treat them other than a methods.
They're not close to methods, if they were they'd accept
parameters. (Ok, the indexer property accepts a parameter). So they can't be threated as methods, more like fields, or better: virtual fields.


I think they're much more like methods than parameters. Consider:

o They end up as methods internally


but that's not important to the developer.
o They can be virtual, overridden etc
o They *can* take parameters, as indexers - although C# is quite
restrictive in terms of its indexers, compared with other .NET
languages.
o They can't be used as ref/out parameters

So, in what way are they like fields *apart* from in access syntax?


In their semantic behaviour, more or less:

int val = Foo.Bar; // A
int val = Foo.Bar; // B
int val = Foo.Bar(); // C

is A a property or public field? Or is B? C is a method, no
question about it. As MS says: if it takes a lot of time, make it a method
instead of a property. Properties are mostly used in scenario's where you
want a value, which is retrieved with very little logic, or to set a value
in a class.

They can't be used like you'd use fields, agreed, that's why they
shouldn't be seen as fields, but properties. After all, properties can
contain additional logic like conversion code, checks and what have you.
Fields don't. However for the user of the property, that's not important.

One clear separation of properties from methods has been left
undiscussed: properties can appear at the left-hand side of an expression,
methods can't.

FB

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #11
"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in news:
#B**************@tk2msftngp13.phx.gbl:
"Jon Skeet [C# MVP]" wrote...
I think they're much more like methods than parameters. Consider:
o They *can* take parameters, as indexers - although C#
is quite restrictive in terms of its indexers, compared
with other .NET languages.
You can even extend that argument, as *all* properties with a setter can
make use of the default argument: value.


the property's value, I don't see that as a default argument of a
method, as methods can't have default arguments.
So it definitely is closer to methods than fields.
methods can't be used at the left-hand side of an expression:

Foo.Bar() = 10; // *rrrt* :)
Foo.Bar = 10; // *ping!*

.. so using that they have to be closer to fields than methods! ;)

I think "closer to ABC" is a bit academic. They're nor fields nor
methods, they're properties.
Furthermore, even Microsoft obviously consider properties in terms of
methods. If you reverse engineer a class into UML with the combination
VS.NET/Visio, properties show up in the operation section of the class!


Yes, this is interesting :) Below the surface they have to be
transformed into methods apparently. Which is not that strange, as it
would be impossible to use a C# class with properties in a J# application,
or in a piece of MC++ code

FB

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #12
Frans Bouma [C# MVP] <pe******************@xs4all.nl> wrote:
I think they're much more like methods than parameters. Consider:

o They end up as methods internally
but that's not important to the developer.


I think it's something all developers should be aware of, myself.
o They can be virtual, overridden etc
o They *can* take parameters, as indexers - although C# is quite
restrictive in terms of its indexers, compared with other .NET
languages.
o They can't be used as ref/out parameters

So, in what way are they like fields *apart* from in access syntax?


In their semantic behaviour, more or less:

int val = Foo.Bar; // A
int val = Foo.Bar; // B
int val = Foo.Bar(); // C


Less like fields than methods, in my opinion.
is A a property or public field? Or is B? C is a method, no
question about it. As MS says: if it takes a lot of time, make it a method
instead of a property. Properties are mostly used in scenario's where you
want a value, which is retrieved with very little logic, or to set a value
in a class.
Mostly, yes. But you need to be aware that they *are* method calls.
They can't be used like you'd use fields, agreed, that's why they
shouldn't be seen as fields, but properties. After all, properties can
contain additional logic like conversion code, checks and what have you.
Fields don't. However for the user of the property, that's not important.

One clear separation of properties from methods has been left
undiscussed: properties can appear at the left-hand side of an expression,
methods can't.


True, but again, that's just a difference in syntax:

Property = value;

is just translated to:

SetValue (value);

There's a clear translation from any use of properties to the
equivalent use of methods. Anything you can do with a property could be
done with a method. The same is *not* true of fields.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
Frans Bouma [C# MVP] <pe******************@xs4all.nl> wrote:
> I think they're much more like methods than parameters. Consider:
>
> o They end up as methods internally


but that's not important to the developer.


I think it's something all developers should be aware of, myself.


Could you elaborate on this, please? I don't see why developers
should be aware of this that much.
> o They can be virtual, overridden etc
> o They *can* take parameters, as indexers - although C# is quite
> restrictive in terms of its indexers, compared with other .NET
> languages.
> o They can't be used as ref/out parameters
>
> So, in what way are they like fields *apart* from in access syntax?


In their semantic behaviour, more or less:

int val = Foo.Bar; // A
int val = Foo.Bar; // B
int val = Foo.Bar(); // C


Less like fields than methods, in my opinion.


erm, there is no difference between A and B, and still you say B
(or A ;)) is more like C than B is to A? :)
is A a property or public field? Or is B? C is a method, no
question about it. As MS says: if it takes a lot of time, make it a
method instead of a property. Properties are mostly used in scenario's
where you want a value, which is retrieved with very little logic, or
to set a value in a class.


Mostly, yes. But you need to be aware that they *are* method calls.


because they can take a given amount of time due to the logic
inside the methods? If that's your argument, then I agree.
They can't be used like you'd use fields, agreed, that's why
they
shouldn't be seen as fields, but properties. After all, properties can
contain additional logic like conversion code, checks and what have
you. Fields don't. However for the user of the property, that's not
important.

One clear separation of properties from methods has been left
undiscussed: properties can appear at the left-hand side of an
expression, methods can't.


True, but again, that's just a difference in syntax:

Property = value;

is just translated to:

SetValue (value);

There's a clear translation from any use of properties to the
equivalent use of methods. Anything you can do with a property could be
done with a method. The same is *not* true of fields.


Ok, semantically, there is indeed not a need for properties, as you
can formulate the get and set as 2 methods, agreed. Just to nittpick: if I
create 2 delegates for the get and set methods, can't I mimic everything I
can do with a field? (ok not left-hand-sides of expressions)

FB

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #14
Frans Bouma [C# MVP] <pe******************@xs4all.nl> wrote:
I think it's something all developers should be aware of, myself.


Could you elaborate on this, please? I don't see why developers
should be aware of this that much.


Well, they need to be aware that they can make properties do more than
return/set fields (which has come up as a question before now, so it's
not as obvious as one might thing), and that also others may make
properties do more than that too.
> So, in what way are they like fields *apart* from in access syntax?

In their semantic behaviour, more or less:

int val = Foo.Bar; // A
int val = Foo.Bar; // B
int val = Foo.Bar(); // C


Less like fields than methods, in my opinion.


erm, there is no difference between A and B, and still you say B
(or A ;)) is more like C than B is to A? :)


They're more like fields in syntax, as I've said before. I regard
sematnics as more important than syntax, however.
Mostly, yes. But you need to be aware that they *are* method calls.


because they can take a given amount of time due to the logic
inside the methods? If that's your argument, then I agree.


Not just in terms of time, but in terms of what they can do.
There's a clear translation from any use of properties to the
equivalent use of methods. Anything you can do with a property could be
done with a method. The same is *not* true of fields.


Ok, semantically, there is indeed not a need for properties, as you
can formulate the get and set as 2 methods, agreed. Just to nittpick: if I
create 2 delegates for the get and set methods, can't I mimic everything I
can do with a field? (ok not left-hand-sides of expressions)


No. You can't use them as output or reference parameters. They won't
have any effect on garbage collection, even if their types are
reference types. They don't (in themselves) have any effect on the size
of an object. The latter two aren't so much things that you can do with
a field so much as effects the fields have, admittedly.

Properties are just syntactic sugar for methods, basically, with a bit
of extra support in the reflection libraries. They make methods *look*
like fields, syntactically - but they still *behave* like methods.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #15

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
In that case, then, I would say that properties should be done away with
and
java like GetXXX and SetXXX should be used instead. A property *is not* a
method as far as the langauge is concerned. Its a field with accessors
that
happen to be implemented as methods in the runtime.
Here I disagree. It's far closer to a method call than a field, to my
mind. It has the syntax of a field, but the semantics of a method call,
which is why you can't use a property as a ref parameter, for instance.


I mean much more as a concept. It is a method call as far as implementation
goes, plain and simple, which is why you can't use a ref parameter(at that,
the C# team could probably have created syntax that would allow a property
with both a get and a set to be passed as a ref or an out parameter, it
would just be more confusing). I consider that more of a restriction of
implementation than of concept. My point is while it is a method, you
shouldn't treat it like a method(as far as usage and syntax goes) or you
will end up defeating the point of properties to begin with. As I said, its
what it is to the langauge(which I consider to be a singular field-like
entity with watchdog\accessor code associated) moreso than to the CLR(which
is a set of methods comprising access to the underlying data). While it must
be treated as a method(or atleast considered as such) much of the time in
reality, I think the language should stick to a more conceptual view. --
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #16
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
I mean much more as a concept. It is a method call as far as implementation
goes, plain and simple, which is why you can't use a ref parameter(at that,
the C# team could probably have created syntax that would allow a property
with both a get and a set to be passed as a ref or an out parameter, it
would just be more confusing). I consider that more of a restriction of
implementation than of concept. My point is while it is a method, you
shouldn't treat it like a method(as far as usage and syntax goes) or you
will end up defeating the point of properties to begin with. As I said, its
what it is to the langauge(which I consider to be a singular field-like
entity with watchdog\accessor code associated) moreso than to the CLR(which
is a set of methods comprising access to the underlying data). While it must
be treated as a method(or atleast considered as such) much of the time in
reality, I think the language should stick to a more conceptual view.


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.

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #17

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
I mean much more as a concept. It is a method call as far as
implementation
goes, plain and simple, which is why you can't use a ref parameter(at
that,
the C# team could probably have created syntax that would allow a
property
with both a get and a set to be passed as a ref or an out parameter, it
would just be more confusing). I consider that more of a restriction of
implementation than of concept. My point is while it is a method, you
shouldn't treat it like a method(as far as usage and syntax goes) or you
will end up defeating the point of properties to begin with. As I said,
its
what it is to the langauge(which I consider to be a singular field-like
entity with watchdog\accessor code associated) moreso than to the
CLR(which
is a set of methods comprising access to the underlying data). While it
must
be treated as a method(or atleast considered as such) much of the time in
reality, I think the language should stick to a more conceptual view.
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++. --
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #18
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
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


But those qualities *are* just the syntactic qualities - they're just
easier syntax for calling the methods. In what way *isn't* that
syntactic sugar, albeit very "good" sugar? As you say later on, there's
extra CLR support for properties, but I would say that at least
*nearly* all the stuff that makes them appear field-like is just
syntactic sugar.
(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).
Agreed :)
As Frans said in another post in this
thread(paraphrased), a field is a field, a method a method, and a property a
property.
And I'd be happy with that - it's when people claim that a property is
more "field-like" than "pair-of-methods-like" that I have problems,
because it's *only* the syntax which is field-like, not the semantics.
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.
Sure.

<snip agreement stuff>
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).
That's fine - the fact that they're compiled to methods is all that I
would particularly urge people to know. The fact that they have
semantics which are very close to methods is important - the rest is
only important to a few people who are either doing low-level stuff or
are just incorrigibly nosy, like ourselves. :)
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++.


Yes, there definitely more to it than *just* syntactic sugar -
apologies for the previous over-generalisation. The bits that make them
look like fields are just syntactic sugar though, IMO :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #19
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
Frans Bouma [C# MVP] <pe******************@xs4all.nl> wrote:
> I think it's something all developers should be aware of, myself.
Could you elaborate on this, please? I don't see why
developers should be aware of this that much.


Well, they need to be aware that they can make properties do more than
return/set fields (which has come up as a question before now, so it's
not as obvious as one might thing), and that also others may make
properties do more than that too.


ok, as long as the developer also realises that a property
shouldn't do too much (or take too much time). (unless databinding
scenario's force you to)
> There's a clear translation from any use of properties to the
> equivalent use of methods. Anything you can do with a property could
> be done with a method. The same is *not* true of fields.


Ok, semantically, there is indeed not a need for properties,
as you
can formulate the get and set as 2 methods, agreed. Just to nittpick:
if I create 2 delegates for the get and set methods, can't I mimic
everything I can do with a field? (ok not left-hand-sides of
expressions)


No. You can't use them as output or reference parameters. They won't
have any effect on garbage collection, even if their types are
reference types. They don't (in themselves) have any effect on the size
of an object. The latter two aren't so much things that you can do with
a field so much as effects the fields have, admittedly.


Can't I pass a delegate pointing to a SetValue() method which in
fact does the same as setting a field's value? (Academic example). But
indeed, you can't mimic everything.
Properties are just syntactic sugar for methods, basically, with a bit
of extra support in the reflection libraries. They make methods *look*
like fields, syntactically - but they still *behave* like methods.


I wouldn't go that far to call them syntactical sugar, as they're
somewhat essential for databinding for example, as you can't bind to a
pair of methods. (or you have to create a property descriptor object which
calls these methods, but that almost looks like a delegate setting a field
just for the sake of the fact that you can ;)). Where do you draw the
line? I find everything that is generated by the compiler not part of the
language, otherwise you can call a lot of stuff syntactic sugar :)
(foreach for example)

It's indeed a vague area, the behaviour is indeed in general more
like methods, the usage is more like fields. I think as long as developers
realize that properties can do more than fields and that, normally, a
property will do very little behind the scenes (otherwise it would have
been a method) and should do little behind the scenes, the question what
they really are is not that important, as IMHO for both situations (they
are more like fields (usage), noooo they are more like methods
(construction, compiler output)) have good arguments (imho)

FB

Nov 16 '05 #20

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
> 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


But those qualities *are* just the syntactic qualities - they're just
easier syntax for calling the methods. In what way *isn't* that
syntactic sugar, albeit very "good" sugar? As you say later on, there's
extra CLR support for properties, but I would say that at least
*nearly* all the stuff that makes them appear field-like is just
syntactic sugar.


I'll agree, with the caveat that a property is there strictly to provide
field like access, which I consider a bit ironic. A property is a set of
methods that exist so that languages can treat them like fields
syntactically
(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).


Agreed :)
As Frans said in another post in this
thread(paraphrased), a field is a field, a method a method, and a
property a
property.


And I'd be happy with that - it's when people claim that a property is
more "field-like" than "pair-of-methods-like" that I have problems,
because it's *only* the syntax which is field-like, not the semantics.


In the language, which is what the original post was about at that, syntax
tends to matter a bit more than semantics. In programming the semantics
matter more.
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.


Sure.

<snip agreement stuff>
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).


That's fine - the fact that they're compiled to methods is all that I
would particularly urge people to know. The fact that they have
semantics which are very close to methods is important - the rest is
only important to a few people who are either doing low-level stuff or
are just incorrigibly nosy, like ourselves. :)


Being nosy has its perks, ;)
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++.
Yes, there definitely more to it than *just* syntactic sugar -
apologies for the previous over-generalisation. The bits that make them
look like fields are just syntactic sugar though, IMO :)


Yes, that I'll agree on.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #21
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
> 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


But those qualities *are* just the syntactic qualities - they're just
easier syntax for calling the methods. In what way *isn't* that
syntactic sugar, albeit very "good" sugar? As you say later on, there's
extra CLR support for properties, but I would say that at least
*nearly* all the stuff that makes them appear field-like is just
syntactic sugar.


But these syntactic qualities reach very far. You can bind a
collection with object instances of a class with properties to a grid
without having to implement ITypedList to produce custom property
descriptors which call into the methods.

It's easier, but not just 'easier', it helps tremendously in some
areas as the usage of classes in a data-binding scenario can become very
complex (ITypedList is no picknick)

So this sugar tastes really good :) (and is IMHO far less sugar
than, say, foreach)
As Frans said in another post in this
thread(paraphrased), a field is a field, a method a method, and a
property a property.


And I'd be happy with that - it's when people claim that a property is
more "field-like" than "pair-of-methods-like" that I have problems,
because it's *only* the syntax which is field-like, not the semantics.


Agreed! :)

FB

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #22
> 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.

The whole OOP itself (objects, interfaces,virtual methods..) is just
syntactic sugar. You could do everything by yourself, building your own
vtables and so on.
All higher level programming languages are only syntactical sugar that makes
inputting machine instructions in the computer easier than per hand.

But syntactical sugar is good when it makes the code more readable and
selfexplanory so idea of properties looking like fields is not that bad.
The only problem is that you never know wheather a call to a certain propery
is a simple field access internally or wheather the property most do
expensive calculation to return you the value you want. But the same problem
you would have with an old school getter method so who cares.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #23
Wow!

I didn't expect that this thread discussion takes so far...
far... from my question.

Really i want to ask You (group) for personal feeling
of new concept of Properties syntax.

My todays "C# code rules" are standing before serious question:
where the new "two accesors" properties would fit?

Marcin

PS: I know that my english is weak... but i can try to
explain any question :)
Nov 16 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Marcin Grzębski | last post by:
I red MSDN article of C# 2.0 this week... and i found very strange syntax for properties e.g.: public int MyIntValue { get { // ... } protected set { // ... }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.