471,066 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 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 5385
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Marcin Grzębski | last post: by
11 posts views Thread by Kondapanaidu | last post: by
13 posts views Thread by Sam Kong | last post: by
11 posts views Thread by Helmut Jarausch | last post: by
20 posts views Thread by Artur Siekielski | last post: by
17 posts views Thread by David C. Ullrich | last post: by
22 posts views Thread by Peter | last post: by
reply views Thread by leo001 | last post: by

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.