473,407 Members | 2,546 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,407 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 5637
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.