473,238 Members | 3,037 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,238 software developers and data experts.

C# properties: nothing more syntactic sugar for getters and setters

Hello,

It seems to me that C# properties are nothing more than syntactic sugar for
getters and setters. I wonder whether others hold a different point of view.
Basically, what more do they have to offer?

Thank you for your replies,

Best Regards,

Neil
Nov 16 '05 #1
13 5621
Neil Zanella <nz******@cs.mun.ca> wrote:
It seems to me that C# properties are nothing more than syntactic sugar for
getters and setters. I wonder whether others hold a different point of view.
Basically, what more do they have to offer?


Nothing - just as the using statement (and directive, come to that)
offers nothing more than syntactic sugar. Of course, all of these give
incredibly *useful* syntactic sugar...

Actually, there *is* a difference between having a property and just
having a getter and a setter - the CLR knows it's a property too, so
it's exposed in reflection as a property.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
Hello,

It seems to me that C# properties are nothing more than syntactic sugar for getters and setters. I wonder whether others hold a different point of view. Basically, what more do they have to offer?

Thank you for your replies,

Best Regards,

Neil


What bullshit.
Nov 16 '05 #3
Jon Skeet [C# MVP] wrote:
Neil Zanella <nz******@cs.mun.ca> wrote:
It seems to me that C# properties are nothing more than syntactic sugar for
getters and setters. I wonder whether others hold a different point of view.
Syntactic suger runs many languages: "for(s1; e; s2) s3;" is syntactic
sugar for "s1; while(e) { s3; s2; }", but very usefull.

The "using(T t = e) { s1;...;sN; }" is (almost, since it can also uses
Close()) syntactic sugar for "T t = t; try { s1;...;sN; } finally {
t.Dispose(); }"
Basically, what more do they have to offer?


They provide a way to declare "fields" in interfaces, (like .Count)

They provide a way to just declare a public field right now, you can
always add some code to verify the updates of the field later without
changing the syntax of the code using the field.

Also, the type-system on them is a bit different than on get/set, since
a property is "one" entity and get/set are two functions with separate
types.

This is importent when declaring interfaces:

interface Foo { int i { get; } }

Declares an interface where NO implementation of Foo can have a "set"
part for i. While this is not really a usefull restriction declare, it
is a rather annoying restriction to have applied to ones classes :)

A (slightly confusing maybe) workaround for the restriction is:

class Bar: Foo
{
int Foo.i { get { return i; } }
public int i { get { ... } set { ...} }
}

Which lets people set i with "bar.i = 5" if they know bar as an instance
of Bar, but not if they know it as Foo.

--
Helge
Nov 16 '05 #4
Helge Jensen <he**********@slog.dk> wrote:

<snip>
Also, the type-system on them is a bit different than on get/set, since
a property is "one" entity and get/set are two functions with separate
types.

This is importent when declaring interfaces:

interface Foo { int i { get; } }

Declares an interface where NO implementation of Foo can have a "set"
part for i.


I don't think this is true. For instance:

using System;
using System.IO;
using System.Text;

interface IProperty
{
int IntProperty
{
get;
}
}

class Test : IProperty
{
int intProperty;

public int IntProperty
{
get { return intProperty; }
set { intProperty = value; }
}

static void Main()
{
Test t = new Test();
t.IntProperty = 10;
Console.WriteLine (t.IntProperty);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
> Actually, there *is* a difference between having a property and just
having a getter and a setter - the CLR knows it's a property too, so
it's exposed in reflection as a property.


I think this is an important distinction that we don't emphasize enough
during our weekly visits to this issue.

My understanding from reading the langauge history, is that properties and
events are in the language mostly to support component-oriented programming.
They are also convenient, so that's a nice added bonus. I prefer them to
writing BeanInfo classes like they do in the Java world.

I would also argue that they make the programmers intent more obvious and
the code is therefore more self-describing.
Nov 16 '05 #6
Jon Skeet [C# MVP] wrote:
interface Foo { int i { get; } }

Declares an interface where NO implementation of Foo can have a "set"
part for i.

I don't think this is true. For instance:


You are quite right. As this (rather smaller) test shows in my compiler:

interface Foo { int i { get; } }
class Bar: Foo { public int i { get { return 0; } set { int _i =
value; } } }

But I'm pretty sure I had this problem once, and that FxCop also
reported it to me.

Anyone got anything to add here, did it use to be a problem at some
point in the past?

--
Helge
Nov 16 '05 #7
Properties are syntactically much more natural to use and more
self-documenting than get/set methods. The data in a user defined type
should act like data and be addressed like data, rather than like a
behavior.

--Bob

"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
Hello,

It seems to me that C# properties are nothing more than syntactic sugar
for
getters and setters. I wonder whether others hold a different point of
view.
Basically, what more do they have to offer?

Thank you for your replies,

Best Regards,

Neil

Nov 16 '05 #8
Properties [can] keep all the code that deals
with a specific field "in close proximity" so
when a change is made to 'get', the appropriate
change can be made to 'set' without having to
do much of a search of the rest of the code body.

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
Hello,

It seems to me that C# properties are nothing more than syntactic sugar
for
getters and setters. I wonder whether others hold a different point of
view.
Basically, what more do they have to offer?

Thank you for your replies,

Best Regards,

Neil

Nov 16 '05 #9
"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
It seems to me that C# properties are nothing more than syntactic sugar for getters and setters. I wonder whether others hold a different point of view. Basically, what more do they have to offer?


They offer syntactic sugar. Why isn't that good enough?

One could argue that all programming language are just syntactic sugar
over assembly language (which is itself syntactic sugar over machine code).

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 16 '05 #10
Exactly.
Another reason is that you can quite easily determine whether a property is
readonly by getting the PropertyInfo and checking the CanWrite property --
it would be far more difficult to do that if you were using 'get' and 'set'
methods.

In fact, assuming they're comparing to Java, there isn't even any reflection
in Java so you couldn't do it at all.

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/
"James Curran" <Ja*********@mvps.org> wrote in message
news:O%***************@TK2MSFTNGP12.phx.gbl...
"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
It seems to me that C# properties are nothing more than syntactic sugar for
getters and setters. I wonder whether others hold a different point of

view.
Basically, what more do they have to offer?


They offer syntactic sugar. Why isn't that good enough?

One could argue that all programming language are just syntactic sugar
over assembly language (which is itself syntactic sugar over machine

code).
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Nov 16 '05 #11
Sorry, but there is reflection in Java. Look at the java.lang.reflect namespace

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Exactly.
Another reason is that you can quite easily determine whether a property is
readonly by getting the PropertyInfo and checking the CanWrite property --
it would be far more difficult to do that if you were using 'get' and 'set'
methods.

In fact, assuming they're comparing to Java, there isn't even any reflection
in Java so you couldn't do it at all.
Nov 16 '05 #12
oops, i haven't used java for over 6 years and i didn't recall it being in
there back then but i may have missed it... sorry!

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:eS**************@TK2MSFTNGP10.phx.gbl...
Sorry, but there is reflection in Java. Look at the java.lang.reflect namespace
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Exactly.
Another reason is that you can quite easily determine whether a property is readonly by getting the PropertyInfo and checking the CanWrite property -- it would be far more difficult to do that if you were using 'get' and 'set' methods.

In fact, assuming they're comparing to Java, there isn't even any reflection in Java so you couldn't do it at all.

Nov 16 '05 #13
John Wood <sp**@isannoying.com> wrote:
oops, i haven't used java for over 6 years and i didn't recall it being in
there back then but i may have missed it... sorry!


It's been present since Java 1.1, which shipped in Feb 97.

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

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

Similar topics

2
by: Lachlan Hunt | last post by:
Hi, In JavaScript 1.5, objects can use special getter and setter functions for properties. However, these only seem to be implemented in Gecko and, AFAICT, don't seem to be part of ECMAScript. ...
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 { // ... }
11
by: Kondapanaidu | last post by:
Hi, What is the difference between Properties and functions. Why dont we go for functions instead of properties. Regrads
13
by: Sam Kong | last post by:
Hi, While discussing C#'s using statement, a guy and I had an argument. In C# spec (15.13), there's an explanation like the following. using (R r1 = new R()) { r1.F(); } is precisely...
11
by: Helmut Jarausch | last post by:
Hi, are decorators more than just syntactic sugar in python 2.x and what about python 3k ? How can I find out the predefined decorators? Many thanks for your help, Helmut Jarausch
20
by: Artur Siekielski | last post by:
Hi. I would like to have declarative properties in Python, ie. something like slots definitions in defclass in Common Lisp. It seems that even Java will have it, using a library (...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
22
by: Peter | last post by:
Hi I was wondering, why use properties instead of get and set methods? I have a colleague who dislikes properties - and much prefers to write "string GetName()" and "void SetName(string name)"...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.