473,789 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
23 2074
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in
news:MP******** *************** @msnews.microso ft.com:
Frans Bouma [C# MVP] <pe************ ******@xs4all.n l> 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**********@D oNotSpam.hotmai l.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.n l> 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.co m>
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.co m> wrote in
news:MP******** *************** *@msnews.micros oft.com:
Frans Bouma [C# MVP] <pe************ ******@xs4all.n l> 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.n l> 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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\access or 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.co m>
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\access or 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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\access or 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(Assoc iated 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(paraphra sed), 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.co m>
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(Assoc iated 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(paraphra sed), 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.co m>
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.co m> wrote in
news:MP******** *************** *@msnews.micros oft.com:
Frans Bouma [C# MVP] <pe************ ******@xs4all.n l> 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

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

Similar topics

23
391
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 { // ... }
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.