470,596 Members | 1,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

Simplified property syntax?

WXS
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a backing
field, plus compatibility to later change the implementation for that field
if needed without breaking the interface.
Mar 14 '06 #1
13 1694
> Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}
What if instead you could write:

class MyClass {
public property int Field;
}
Is something like this syntax available? If not should it be? It
seems this is much cleaner and prevents forcing writing a backing
store for each property seperately.

The benefit of this is cleaner coding for properties that just use a
backing field, plus compatibility to later change the implementation
for that field if needed without breaking the interface.


I'm sure I'm missing something obvious here but wouldn't the only difference
between:

public int Field;

and the hypothetical:

public property int Field;

be that Reflection would tag one as a field and the other as a property?
Would there actually be anything you could change with this and thus "change
the interface"?

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Mar 14 '06 #2

"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> wrote in message
news:a0*************************@news.microsoft.co m...
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}
What if instead you could write:

class MyClass {
public property int Field;
}
Is something like this syntax available? If not should it be? It
seems this is much cleaner and prevents forcing writing a backing
store for each property seperately.

The benefit of this is cleaner coding for properties that just use a
backing field, plus compatibility to later change the implementation
for that field if needed without breaking the interface.


I'm sure I'm missing something obvious here but wouldn't the only
difference between:

public int Field;

and the hypothetical:

public property int Field;

be that Reflection would tag one as a field and the other as a property?
Would there actually be anything you could change with this and thus
"change the interface"?


Properties are implemented as methods (set_Field and get_Field in this
case). The compiler would need to create these and the underlying field.

Personaly I can live with the extra typing.
Mar 14 '06 #3
Hi,

And what exactly is the difference between your proposed feature and making
your variable public?

"WXS" <WX*@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems
this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a
backing
field, plus compatibility to later change the implementation for that
field
if needed without breaking the interface.

Mar 14 '06 #4
WXS
When you have a lot of properties that is a lot of extra code and typing, and
unless using a tool you could even cut and paste in the wrong backing store
for a property. It just seems something like this would be cleaner since it
is a fairly common idiom to have a backing store variable.

"Nick Hounsome" wrote:

"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> wrote in message
news:a0*************************@news.microsoft.co m...
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}
What if instead you could write:

class MyClass {
public property int Field;
}
Is something like this syntax available? If not should it be? It
seems this is much cleaner and prevents forcing writing a backing
store for each property seperately.

The benefit of this is cleaner coding for properties that just use a
backing field, plus compatibility to later change the implementation
for that field if needed without breaking the interface.


I'm sure I'm missing something obvious here but wouldn't the only
difference between:

public int Field;

and the hypothetical:

public property int Field;

be that Reflection would tag one as a field and the other as a property?
Would there actually be anything you could change with this and thus
"change the interface"?


Properties are implemented as methods (set_Field and get_Field in this
case). The compiler would need to create these and the underlying field.

Personaly I can live with the extra typing.

Mar 14 '06 #5
WXS
If you make your variable public you don't have the option later to change
your implementation, since the compiler/CLR would allow direct access to the
field. By saying it is a property it will actual compile to get_ set_
methods so that later if you wanted to make a change where a simple backing
store variable no longer was viable you could without breaking existing code
that used your class.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

And what exactly is the difference between your proposed feature and making
your variable public?

"WXS" <WX*@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems
this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a
backing
field, plus compatibility to later change the implementation for that
field
if needed without breaking the interface.


Mar 14 '06 #6
You have this option in C++/CLI.
It's very handy for cases where the get and set are both single lines. You
aren't the only one who finds typing 11 lines for a 'dumb' property annoying
(counting the braces...).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"WXS" wrote:
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a backing
field, plus compatibility to later change the implementation for that field
if needed without breaking the interface.

Mar 14 '06 #7
WXS
Thanks for the reminder on the C++/CLI-- I thought I remembered something
like this but couldn't find it on the C# side... forgot it was C++/CLI.
Certainly C# could use this.
"David Anton" wrote:
You have this option in C++/CLI.
It's very handy for cases where the get and set are both single lines. You
aren't the only one who finds typing 11 lines for a 'dumb' property annoying
(counting the braces...).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"WXS" wrote:
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a backing
field, plus compatibility to later change the implementation for that field
if needed without breaking the interface.

Mar 14 '06 #8
It's even more of a relief in C++/CLI since normally you have to repeat the
return type in three places in a C++/CLI long-hand property - property
header, get, and the set (never could figure out the logic behind that).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"WXS" wrote:
Thanks for the reminder on the C++/CLI-- I thought I remembered something
like this but couldn't find it on the C# side... forgot it was C++/CLI.
Certainly C# could use this.
"David Anton" wrote:
You have this option in C++/CLI.
It's very handy for cases where the get and set are both single lines. You
aren't the only one who finds typing 11 lines for a 'dumb' property annoying
(counting the braces...).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"WXS" wrote:
Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a backing
field, plus compatibility to later change the implementation for that field
if needed without breaking the interface.

Mar 14 '06 #9

"WXS" <WX*@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com...
When you have a lot of properties that is a lot of extra code and typing,
and
unless using a tool you could even cut and paste in the wrong backing
store
for a property. It just seems something like this would be cleaner since
it
is a fairly common idiom to have a backing store variable.
If you have that many properties it is probably a poor OO design.

In a good OO design you ask objects to do things for you - you don't grab
all their data and do it yourself.

"Nick Hounsome" wrote:

"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> wrote in message
news:a0*************************@news.microsoft.co m...
>> Is there or should there be a simplified property syntax?
>>
>> For example we have to write:
>> class MyClass {
>> private int _field;
>> public int Field { get { return _field;} set { _field=value}}
>> }
>> What if instead you could write:
>>
>> class MyClass {
>> public property int Field;
>> }
>> Is something like this syntax available? If not should it be? It
>> seems this is much cleaner and prevents forcing writing a backing
>> store for each property seperately.
>>
>> The benefit of this is cleaner coding for properties that just use a
>> backing field, plus compatibility to later change the implementation
>> for that field if needed without breaking the interface.
>>
>
> I'm sure I'm missing something obvious here but wouldn't the only
> difference between:
>
> public int Field;
>
> and the hypothetical:
>
> public property int Field;
>
> be that Reflection would tag one as a field and the other as a
> property?
> Would there actually be anything you could change with this and thus
> "change the interface"?


Properties are implemented as methods (set_Field and get_Field in this
case). The compiler would need to create these and the underlying field.

Personaly I can live with the extra typing.

Mar 14 '06 #10
WXS <WX*@discussions.microsoft.com> wrote:
Is there or should there be a simplified property syntax?


<snip>

Yes, I believe there should be, for the reasons you've given. In
addition, I'd like to be able to declare a field "inside" a property
declaration so that it's only available within that property (but has
the same lifetime as if it were declared outside). That would prevent
the field from accidentally being used within the class where the
property should be used instead. There are a few gotchas with this
approach, mostly involving serialization, but I think it would be
useful.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 14 '06 #11
WXS
I'd agree with that as well. Is there an official suggestion submission
location we can send them to for MS?

"Jon Skeet [C# MVP]" wrote:
WXS <WX*@discussions.microsoft.com> wrote:
Is there or should there be a simplified property syntax?


<snip>

Yes, I believe there should be, for the reasons you've given. In
addition, I'd like to be able to declare a field "inside" a property
declaration so that it's only available within that property (but has
the same lifetime as if it were declared outside). That would prevent
the field from accidentally being used within the class where the
property should be used instead. There are a few gotchas with this
approach, mostly involving serialization, but I think it would be
useful.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 14 '06 #12
WXS <WX*@discussions.microsoft.com> wrote:
I'd agree with that as well. Is there an official suggestion submission
location we can send them to for MS?


I submitted that one ages ago, although more voices wouldn't hurt.

You could make a full feature request on the MSDN product feedback
centre:

http://lab.msdn.microsoft.com/produc...k/default.aspx

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 14 '06 #13
WXS
Thanks I added one.

"Jon Skeet [C# MVP]" wrote:
WXS <WX*@discussions.microsoft.com> wrote:
I'd agree with that as well. Is there an official suggestion submission
location we can send them to for MS?


I submitted that one ages ago, although more voices wouldn't hurt.

You could make a full feature request on the MSDN product feedback
centre:

http://lab.msdn.microsoft.com/produc...k/default.aspx

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 14 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Stephan Kurpjuweit | last post: by
5 posts views Thread by Ahmad Jalil Qarshi | last post: by
3 posts views Thread by James Allen Bressem | last post: by
reply views Thread by Memfis | last post: by
3 posts views Thread by bearophileHUGS | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.