views:

341

answers:

4

Looking at the new VB 2010 features, I stumbled upon support for Auto-Implemented Properties.

Since I'm working with C#, this seemed quite familiar, but I noticed that VB did add a feature I would love to have in C#: setting a arbitrary default value for the auto-implemented property:

Auto-implemented property with default value

I really like the clean usage of auto-properties in C#. This would save us the effort of introducing a backing field and hooking it up to the property everytime we simply need a default value, thereby cluttering up the code unnecessarily.

I was wondering why this wasn't introduced in C# as well? What could be the rationale for not doing this? Is a syntax discussion going on, or are there technical limitations to implementing this?

+9  A: 

Why not just default them in the constructor? That's what its for too.

Preet Sangha
Exactly. Spot on.
Finglas
That's what I usually do, but sometimes it can be helpful to see the defaults at declaration.
Rob van Groenewoud
You can have multiple constructors, hence multiple defaults. So while it's a good solution, it's not as clean as supplying a single default value.
Jason Williams
@Jason Williams: If you have multiple constructors, you can use the this-initializer to prevent duplicated code: `public Person(string name) : this() {...}`
Abel
In addition to my other comment: You'll have to remember to implement the constructor and use constructor chaining if you have multiple constructors and don't want to duplicate the initialization code. Seems like a bit of a hassle to me.
Rob van Groenewoud
@Preet: This answer provides the preferred way to implement this within the current context, let it be an example for other people!However it doesn't provide a lot of rationale behind this decision which I was asking for. Abel's answer does a little better at explaining the 'why?'. Thanks though.
Rob van Groenewoud
@Abel: Exactly. It would be much easier just to have a default value on the property, wouldn't it :-)
Jason Williams
A: 

Whilst I'm not Microsoft, I would suggest that the perceived benefit was less than the cost to implement, test and support the feature.

Of course, you could just set the default values by declaring a constructor, but I do agree that the VB syntax is a little clearer (especially if you're going to decorate it with metadata such as <DefaultValue(...)>)

Rowland Shaw
+3  A: 

What about:

public class Person
{
    public Person() 
    {
         this.Name = "Scott Guthrie";
         this.Age = 35;
    }
    public string Name { get; set; }
    public string Age { get; set; }
}

in practice, that comes down to the same and isn't that much extra work, I believe. But perhaps, for once in a long while, VB looks clearer then C#... ;-)

EDIT (rationale):
You were asking for the rationale in your last comment under (and in) your original question. Thinking out loud, I think that the principle in C# that initialization code goes to one place and one place only, namely the constructor, is the reason for this decision. Adding another place where you have to look to find initialization code makes debugging harder and the code less clear.

Obviously, an inline initialization value cannot contain other initializations or calculations (at least, very limited). While I agree that it can be more concise in the VB way, I would understand the C# team and Anders Hejlsberg if they said that they consider it a bigger advantage to have one place for initialization.

EDIT: here's what Microsoft says about it. In short, not for C# 4.0, but perhaps C# 5.0? Also:

"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."

and (just a commenter):

"Lack of initialization or constructor control makes the feature practically worthless for properties returning a reference type."

Abel
James Curran comments on that answer by saying - "DefaultValueAttribute does NOT set the value of a property. All it does is tell Visual Studio what the default value should be, so that in the Property Window, the will be bolded if it is not set to that value. It does not change the value in any way"
Jamie Dixon
See comment in other thread: "DefaultValueAttribute does NOT set the value of a property. All it does is tell Visual Studio what the default value should be, so that in the Property Window, the will be bolded if it is not set to that value. It does not change the value in any way."
Rob van Groenewoud
If I'm not mistaken, `DefaultValue` only sets the value for Visual Studio, in the property pane, but does not set the value at execution time.
Shimrod
Ok, thanks guys, three times ;-). I removed the offending section. You are totally right and it makes sense, considering the way attributes normally work. I should've read it through though.
Abel
Thanks for your thoughts and the link to Microsoft's reaction. As I thought, there's simply more to it than just adding extra syntax features. We'll just have to wait for MS to come up with at proper design for this. In the meantime, we'll just have to use the constructor. But I'm still wondering how this works in VB though...
Rob van Groenewoud
The layout of http://vbcity.com/blogs/mike-mcintyre/archive/2009/09/07/visual-basic-2010-auto-implemented-properties-a-closer-look.aspx is a bit shaky, but it perhaps answers some of your questions on VB auto properties
Abel
While the code example is basically what Preet's answer is saying, the edits on this answer did provide me some more insight on the rationale behind this, which I was looking for. Therefore I'll accept this as the answer, at least for now ;-)
Rob van Groenewoud
A: 

I have a solution for the tedious business of converting an auto-property into a property-with-backing-field: My addin, AtomineerUtils will do that refactoring for you with a single keypress.

Jason Williams