PC Review


Reply
Thread Tools Rate Thread

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

 
 
Neil Zanella
Guest
Posts: n/a
 
      31st Dec 2004
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
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      31st Dec 2004
Neil Zanella <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      31st Dec 2004
"Neil Zanella" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Helge Jensen
Guest
Posts: n/a
 
      31st Dec 2004
Jon Skeet [C# MVP] wrote:
> Neil Zanella <(E-Mail Removed)> 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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      31st Dec 2004
Helge Jensen <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?TWFya1Q=?=
Guest
Posts: n/a
 
      31st Dec 2004
> 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.
 
Reply With Quote
 
Helge Jensen
Guest
Posts: n/a
 
      1st Jan 2005
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
 
Reply With Quote
 
Bob Grommes
Guest
Posts: n/a
 
      1st Jan 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Peter N Roth
Guest
Posts: n/a
 
      1st Jan 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
James Curran
Guest
Posts: n/a
 
      4th Jan 2005
"Neil Zanella" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reflection and setters & getters Shmuel Microsoft Dot NET Framework 2 23rd Sep 2008 07:55 AM
XmlSerializer only serializes fields with getters & setters? Ethan Strauss Microsoft C# .NET 5 17th Sep 2008 03:18 PM
are getters/setters a must? Ben Microsoft C# .NET 5 10th Mar 2008 01:09 AM
How about this syntactic candy? Hilton Microsoft C# .NET 43 28th Nov 2007 07:12 PM
About syntactic sugar Sam Kong Microsoft C# .NET 13 29th Sep 2006 09:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:28 PM.