Auto-implemented properties

  • Thread starter Thread starter Jesper, Denmark
  • Start date Start date
J

Jesper, Denmark

Hi,

I think that auto-implemented properties are a wonderful new c# 3.0 feature.

However, anyone who knows how I can set a default value (initial value) for
the property like in the bad old days:

int foo = 5;
public int Foo {get ... yada yada}
 
Jesper --
I think that auto-implemented properties are a wonderful new c# 3.0
feature.
Agreed.

However, anyone who knows how I can set a default value (initial value)
for
the property like in the bad old days:

AFAIK there is no way to set a default value in the same statement as the
declaration.

I would like to see something like the following implemented in the next
iteration of C#
for these properties...

public int Foo { get; set; } = 5;

IMHO these auto-implemented properties simplify property creation and
improve code readability. However having to now provide default value in
the constructor or some other method that is not within proximity of the
declaration
is counter to those goals (simplification and readability.) By providing a
compact
syntax that would allow for assigning a default value with the declaration
we
would have the best of all worlds. :)

I would like to hear others thoughts on this as well.
 
Gregg Walker said:
AFAIK there is no way to set a default value in the same statement as the
declaration.

I would like to see something like the following implemented in the next
iteration of C#
for these properties...

public int Foo { get; set; } = 5;

I don't mind missing default values too much. I just don't like the
fact that you can't make readonly automatic properties.
 
John --
I don't mind missing default values too much. I just don't like the
fact that you can't make readonly automatic properties.

You probably already know this...you can't get a "true" readonly automatic
property as in

public int Foo { get; }

But you can get "same as" readonly behavior with

public int Foo { get; private set; }

or

public int Foo { get; protected set; }

And the same would apply to writeonly properties as well.
 
Gregg Walker said:
You probably already know this...you can't get a "true" readonly automatic
property as in

public int Foo { get; }

But you can get "same as" readonly behavior with

public int Foo { get; private set; }

or

public int Foo { get; protected set; }

Well, it's readonly from outside the class - but:

1) It doesn't stop the code within the class from accidentally setting
the property when it shouldn't

2) It means the backing variable can't be marked readonly, which may
prevent some JIT optimisations.

I would rather have real, true readonly-ness, where the property could
only be set in the constructor (the same as a readonly variable).
And the same would apply to writeonly properties as well.

I'm not as worried about those :)
 
John --
I would rather have real, true readonly-ness, where the property could
only be set in the constructor (the same as a readonly variable).
Agreed.


I'm not as worried about those :)

LOL. My sentiments as well.
 
Gregg said:
However having to now provide default value in
the constructor or some other method that is not within proximity of the
declaration
is counter to those goals (simplification and readability.)

I think it increases readability to do all object construction
in constructor instead of some in constructor and some
in initializations spread out all over the class.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top