Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Auto-implemented properties

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jesper, Denmark - 05 Mar 2008 17:19 GMT
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}
Gregg Walker - 05 Mar 2008 18:08 GMT
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
Jon Skeet [C# MVP] - 05 Mar 2008 18:24 GMT
> > However, anyone who knows how I can set a default value (initial value)
> > for
[quoted text clipped - 8 lines]
>
>   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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Gregg Walker - 05 Mar 2008 18:52 GMT
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
Jon Skeet [C# MVP] - 05 Mar 2008 19:15 GMT
> > I don't mind missing default values too much. I just don't like the
> > fact that you can't make readonly automatic properties.
[quoted text clipped - 11 lines]
>
>   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 :)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Gregg Walker - 05 Mar 2008 19:29 GMT
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.

>> And the same would apply to writeonly properties as well.
>
> I'm not as worried about those :)

LOL.  My sentiments as well.
--
Gregg Walker
Arne Vajhøj - 09 Mar 2008 02:27 GMT
>                         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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.