Home Dashboard Directory Help
Search

Add initialization to automatic properties in C# by Laughing John


Status: 

Closed
 as Won't Fix Help for as Won't Fix


52
0
Sign in
to vote
Type: Suggestion
ID: 361647
Opened: 8/13/2008 8:19:54 AM
Access Restriction: Public
0
Workaround(s)
view

Description

Automatic properties should be a useful shortcut on the many times you need standard property handling with a private backing variable.

However, automatic properties as they are implemented now are next to useless. This is because with reference types you nearly always want to create a new object rather than just leave the reference null. This is particularly noticeable with strings.

Also there is no quick way to refactor an automatic property into a standard property.

I've lost count of the number of times I've used an automatic property then had to convert it to 'standard' property syntax just so I can initialize the private variable.

This is made worse because the "prop" snippet now generates an automatic property and there is no easy way to refactor an automatic property to a standard one. (note: you can add the old snippet back in).

This means that this time saving feature actually introduces more typing.
Details
Sign in to post a comment.
Posted by Daniel Smith on 2/26/2014 at 1:02 AM
It's 2014, and it's been exactly 2,014 days since this suggestion was logged here on Connect! It's nice to see on this special anniversary that the suggestion over on UserVoice has been updated with good news that plans are finally in place :-)

http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2163145-allow-default-values-on-automatic-properties
Posted by llorrac on 2/7/2012 at 7:27 AM
My guess is that the C# team isn't that sharp (pun intended). Perhaps they should ask their colleagues in the VB development area how they managed to do it,
Posted by Laughing John on 2/7/2012 at 3:46 AM
Just wanted to point out that this has now been added by someone to the Visual Studio Suggestions site here: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2163145-allow-default-values-on-automatic-properties

Why we need two sites is beyond me! Maybe so MSFT can forget all the old ones and start again :)
Posted by Aquilax on 11/24/2010 at 1:23 AM
I would like to add it to the c# 5.0 wishlist but I can't find it anywhere.
It would be nice to have the conversion of the auto property be done with a lazy initialization pattern, like something of the type:

public string AutoInitializer { get; } = "Hello World";

it would converted to

private _autoInitializer ;
public string AutoInitializer { get { return _autoInitializer ?? (_autoInitializer = "Hello World"); } }
Posted by Jeromeyers on 7/15/2010 at 8:13 AM
I agree... the concept of an AIP was very nice, so long as we know the best times to use it (FI, not with XmlSerializer)... however, to be able to initialize the AIP is a logical and elegant extension to the concept and I see no reason why this should be a closed issue. VB has it (now)... so should C#.
Posted by Daniel Smith on 8/20/2009 at 8:03 AM
It's incredibly disappointing that VB.NET 4.0 is getting this exact feature, despite no requests on Connect, yet the large number of people who have requested this for C# are completely ignored :-(
Posted by Kyrenia1 on 2/12/2009 at 2:34 PM
This would also provide a way to avoid the mistake of initializing anything in the Setter, when obviously the Getter could be called first.
Posted by George Tsiokos on 10/7/2008 at 11:07 AM
Another request for properties in c#
http://george.tsiokos.com/posts/2008/10/07/c-sharp-language-request-for-properties/
Posted by wayne isaacs on 9/23/2008 at 9:11 AM
Lack of initialization or constructor control makes the feature practically worthless for properties returning a reference type.
Posted by Laughing John on 9/19/2008 at 3:50 AM
Excellent news! Thanks for your time.
Posted by Microsoft on 9/18/2008 at 3:20 PM
Hi LJ,

Thanks again for the suggestion. Regarding your suggestions for the IDE, I'm happy to say that due to the customer feedback we've received, we've added the original prop snippet back in for our next release as "propfull". Additionally, we have heard the suggestion for refactoring auto properties to "normal" properties before and its on our list of features to consider before each release. If you have any further questions/comments about the IDE interaction with auto properties, feel free to shoot me an email directly at djpark@microsoft.com.

Thanks!
DJ park
C# IDE PM
Posted by Laughing John on 9/15/2008 at 5:24 PM
Thanks for looking at this Mads.

I think it's fine if the C# team do not wish to do anything about this as I can continue to use the full property syntax.

My issue is the way that automatic properties have been 'sold' to us by the replacement of the prop snippet (and the marketing). I know we can add it back in but for me it's the snippet I used most often and given the short-comings of automatic properties it seems daft to push us in that direction and effectively slow us down.

Thanks again,
LJ
Posted by Microsoft on 9/15/2008 at 2:22 PM
Thank you for your suggestion!

I'll reply to the language suggestion, and then pass this issue on to the IDE team to comment on the VS suggestions.

Automatically implemented properties are limited in several ways. We were very cautious when we introduced them to not make this an overblown feature. Some may claim that the feature is now "underblown".

One of the features we should consider is initialization like you propose. It is not as easy as it sounds though: the next thing you want is for the constructor to initialize the backing field, but it can only do that through the setter, which might not be what you want.

All that said, we could come up with something. We haven't for the upcoming version of C# and it is unlikely that we will be able to add this feature, as we have a very full plate of features already. However we will track this for discussion in the next release after that.

Thanks again,

Mads Torgersen, C# Language PM
Posted by Pavel Minaev [MSFT] on 8/29/2008 at 2:51 AM
In fact, breakpoints on automatic properties could use a separate ticket.
Posted by Mick Lang on 8/24/2008 at 11:31 PM
I agree. Automated properties are a bit of a trap, if you wish to add an intializer, there's no refactoring tool to convert it to a standard property, in this case you do end up typing twice as much as you would have if you had implemented an ordinary property to begin with. The other trap for automated properties is the inability to set a break point on an automated property's get or set accessor in Visual Studio.
Posted by Grant Trimble on 8/22/2008 at 5:00 AM
Good comment. Sounds a bit like the initializers for generic lists which where missing in C# 2.0 but was added in 3. Please add for 3.2 or 4, if possible
Sign in to post a workaround.