The new c# compiler allow us to write very clean and simplified property declaration, that avoid us to declare the private member and the get/ set accessors. here is the snippet (just do "prop TAB TAB" in visual studio 2008)
public string Name { get; set; }
my original post was wrong (thank for the report), shame on me.
I'll post a fixed version asap.
Here is what to do when you need a default value for your property : Just use our good old attributes:
[System.ComponentModel.DefaultValue("John doe")]
public string Name { get; set; }

3 comments:
This does not work.
See:
http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx
It is intended more to be used when the default value is being set in code for a value different then what the default would be. For instance if you had a boolean but set the vaue to true. It does not set a set default value but only identifies a default value which may or may not be the true default value.
For example:
private string _name = "John Doe";
[System.ComponentModel.DefaultValue("John Doe")]
public string Name {
get { return _name; }
set { _name = value; }
}
I think DefaultValueAttribute scope only Designer ?
so there is no possibility to for the automated properties to assign a default value!!!
How sad is this
Post a Comment