473,383 Members | 1,863 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,383 software developers and data experts.

Simpler syntax for properties?

Does anyone else think it would be nice to be able to write something like
this ...

public SomeType P { v; }

.... rather than this?

public SomeType P
{
get { return v; }
set { v = value; }
}

I realise that the property construct allows 'get' and 'set' to do more than
just assign or return a value, but in most cases - when I don't want to do
anything else - the standard form seems slightly long-winded.

P.
Nov 16 '05 #1
9 2124

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:c5**********@sparta.btinternet.com...
Does anyone else think it would be nice to be able to write something like
this ...

public SomeType P { v; }

... rather than this?

public SomeType P
{
get { return v; }
set { v = value; }
}

I realise that the property construct allows 'get' and 'set' to do more
than
just assign or return a value, but in most cases - when I don't want to do
anything else - the standard form seems slightly long-winded.


I definatly agree, the syntax is long and not as clear as it should be. I
actually may be one of the few that thinks fields should be relegated to
sparse use and that the compiler\language should have made a greater effort
to remove the need for manual declaration of fields.

I've seen some syntax that suggests the addition of a property keyword
and\or a field associative keyword. Personally I like:
public property SomeType P{get;set;} //new keyword
or
public implicit SomeType P{get;set;} // saves a keyword, but overloads an
existing one
or just
public SomeType P{get;set;} //no keyword, but is it as clear as the above
and will people mistake it for abstract?
where the compiler generates its own field to store the backing type. P =
<value> in the declaring class calls set if its available but is transformed
into a field assign by the compiler when the property is get only. Similar
semantics could be performed for get if the property is set only. There is
some problem with that as far as how apparent the behaviour is within the
class. However I find it cleaner than trying to mix get, set, and a field
association into the property.
Nov 16 '05 #2
Paul E Collins wrote:

Does anyone else think it would be nice to be able to write something like
this ...

public SomeType P { v; }

... rather than this?

public SomeType P
{
get { return v; }
set { v = value; }
}

I realise that the property construct allows 'get' and 'set' to do more than
just assign or return a value, but in most cases - when I don't want to do
anything else - the standard form seems slightly long-winded.


What's wrong w/ just exposing the data member then?

public SomeType P;
Nov 16 '05 #3
Julie <ju***@nospam.com> wrote:
What's wrong w/ just exposing the data member then?

public SomeType P;


Everything :)

If you want to later change the implementation, you can't.
If you want to limit access to read or write only, you can't.
If you want to validate values on set, you can't.
If you want to trace use during debug, you can't.

Unless something is effectively constant (whether an actual const or
just something which is static, readonly and immutable), I always keep
it private.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Paul E Collins <fi******************@CL4.org> wrote:
Does anyone else think it would be nice to be able to write something like
this ...


<snip>

Yes. I didn't for a while, but now I definitely do.

Unfortunately, it could get quite messy by the time you specify:

o Whether to have both a get and a set, or only one of them
o What the access levels are for each of them

One other thing about properties which I've asked for before and which
your example syntax seems to suggest is the ability to declare
variables *within* a property, but outside the accessors themselves.
They could be name-mangled slightly to avoid collisions, and would (in
the compiled IL) just be private variables - but within the C# code
itself, only the property would have access to it.

That would mean you could make absolutely sure that *all* code to
modify a particular piece of state went through the property and didn't
tamper with the variables directly.

--
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
"Jon Skeet [C# MVP]" wrote:

Julie <ju***@nospam.com> wrote:
What's wrong w/ just exposing the data member then?

public SomeType P;


Everything :)

If you want to later change the implementation, you can't.
If you want to limit access to read or write only, you can't.
If you want to validate values on set, you can't.
If you want to trace use during debug, you can't.


If I want to later change the implementation, I could simply do this:

private SomeType p; // Was: P

public SomeType P
{
get { /* ... */ }
set { /* ... */ }
}

That solves 1 and 2, right?

The OP didn't indicate the need to validate values to trace in debug. If those
are requirements, then you are right. If not requirements, then what is wrong
w/ my idea?
Nov 16 '05 #6
Julie <ju***@nospam.com> wrote:
If you want to later change the implementation, you can't.
If you want to limit access to read or write only, you can't.
If you want to validate values on set, you can't.
If you want to trace use during debug, you can't.
If I want to later change the implementation, I could simply do this:

private SomeType p; // Was: P

public SomeType P
{
get { /* ... */ }
set { /* ... */ }
}

That solves 1 and 2, right?


No - you've just broken your public interface, so anything using it
needs to be recompiled. Using properties, the public interface stays
the same even if the implementation changes.
The OP didn't indicate the need to validate values to trace in debug. If those
are requirements, then you are right. If not requirements, then what is wrong
w/ my idea?


The above. If the OP's requirements for tracing, validation etc change
(as they often do), using properties provides a backward-compatible way
of working. Fields don't.

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


----- Jon Skeet [C# MVP] wrote: ----
One other thing about properties which I've asked for before and which

your example syntax seems to suggest is the ability to declare
variables *within* a property, but outside the accessors themselves.
They could be name-mangled slightly to avoid collisions, and would (in
the compiled IL) just be private variables - but within the C# code
itself, only the property would have access to it

or even better, alleviate the need to manually declare a private field altogether. introduce a new keyword, they already have *value* right? have something like *state* for example. so you can do something like get { return state; } set { state = value }, and let the compiler handle the declaration of the underlying field in IL. then that will solve the whether to prefix fields with "_" or "m_" debate once and for all. :) added bonus

--
Jon Skeet - <sk***@pobox.com
http://www.pobox.com/~skee
If replying to the group, please do not mail me to

Nov 16 '05 #8

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Julie <ju***@nospam.com> wrote:
> If you want to later change the implementation, you can't.
> If you want to limit access to read or write only, you can't.
> If you want to validate values on set, you can't.
> If you want to trace use during debug, you can't.


If I want to later change the implementation, I could simply do this:

private SomeType p; // Was: P

public SomeType P
{
get { /* ... */ }
set { /* ... */ }
}

That solves 1 and 2, right?


No - you've just broken your public interface, so anything using it
needs to be recompiled. Using properties, the public interface stays
the same even if the implementation changes.


Not to mention that its possible code would break if the field is used as a
ref or out parameter, as has been coming up in these groups alot lately.
Its considerably less likely, but it is an issue, IMHO.
The OP didn't indicate the need to validate values to trace in debug. If
those
are requirements, then you are right. If not requirements, then what is
wrong
w/ my idea?


The above. If the OP's requirements for tracing, validation etc change
(as they often do), using properties provides a backward-compatible way
of working. Fields don't.

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

Nov 16 '05 #9
Daniel Jin <an*******@discussions.microsoft.com> wrote:
or even better, alleviate the need to manually declare a private
field altogether. introduce a new keyword, they already have *value*
right? have something like *state* for example. so you can do
something like get { return state; } set { state = value }, and let
the compiler handle the declaration of the underlying field in IL.
then that will solve the whether to prefix fields with "_" or "m_"
debate once and for all. :) added bonus.


Hmm... not sure about that, except in the very simple case where you
can declare the whole property in one statement.

Not every property will have a single backing variable, and I wouldn't
want a variable to be automatically generated some times but not
others.

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

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

Similar topics

37
by: Bengt Richter | last post by:
ISTM that @limited_expression_producing_function @another def func(): pass is syntactic sugar for creating a hidden list of functions. (Using '|' in place of '@' doesn't change the picture...
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 { // ... }
8
by: Paul E Collins | last post by:
Does anyone else think it would be nice to be able to write something like this ... public SomeType P { v; } .... rather than this? public SomeType P { get { return v; }
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.