Click here to Skip to main content
Click here to Skip to main content

Tagged as

Setting a default value for C# Auto-implemented properties

, 7 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
One of the things missing from C# is the ability to set a default value of auto-implemented properties. You can set a default for variable backed properties, and you can set them for the designer. This does it for you.
If you auto-implement a property, it would be handy to be able to set a default value. Indeed, there is the System.ComponentModel.DefaultValueAttribute which you can set, perfectly happily:
[DefaultValue("-New Object-")]
public string MyString { get; set; }
public int MyInt { get; set; }
public int EnvelopeHeight { get; set; }
public bool MyBool { get; set; }
This compiles perfectly, and off you go.
The only problem is, this only affects the designer - not the runtime properties.
MSDN even makes this clear: DefaultValueAttribute Class[^]
"A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code."
Which means in layman's terms: "This is for the VS designer only - go away and use a backing variable."
No. I won't. I don't want the clutter.
But, they forgot they gave us Reflection...
If in your constructor, you read through the list of your class properties, you can get access to the default values...
public MyClass()
    // Use the DefaultValue propety of each property to actually set it, via reflection.
    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(this))
        DefaultValueAttribute attr = (DefaultValueAttribute)prop.Attributes[typeof(DefaultValueAttribute)];
        if (attr != null)
            prop.SetValue(this, attr.Value);
You won't want to do this in controls: it would override any values you set in the designer!
[edit]Addendum regarding auto-indentation removed - OriginalGriff[/edit]


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?
Follow on   Google+

Comments and Discussions

GeneralMy vote of 5 PinmemberHassen Doghmen12-Sep-14 4:09 
QuestionMessage Automatically Removed PinprofessionalRavi.Kumar0215-Jan-14 22:47 
GeneralMy vote of 1 Pinmembercocowalla22-Jul-13 10:49 
GeneralMy vote of 1 Pinmembervijayksingh20-Mar-13 22:35 
GeneralRe: I've posted the code to what I was describing below for your... PinmemberAndrew Rissing5-Jan-12 4:53 
GeneralRe: It would just rely on what you described above to acquire wh... PinmemberAndrew Rissing4-Jan-12 12:47 
GeneralReason for my vote of 3 I don't really agree this is missing... Pinmemberdojohansen13-Jan-12 4:51 
GeneralAll these "solutions" have no meaning just by one reason: yo... PinmemberMember 308248711-Jan-12 21:33 
GeneralReason for my vote of 5 Excellent thought. Though due to lac... PinmemberAll Time Programming11-Jan-12 19:44 
GeneralReflection is sloooow! Reflection has many great uses. Setti... Pinmembercathal_mchale9-Jan-12 22:45 
GeneralReason for my vote of 5 ok jkhj Pinmemberprakash.rath1239-Jan-12 21:40 
GeneralReason for my vote of 5 Great example of "thinking out of th... PinmemberBillWoodruff9-Jan-12 15:10 
GeneralNeat! :) PinmvpLuc Pattyn4-Jan-12 10:58 
GeneralKewl! PinmemberCollin Jasnoch4-Jan-12 9:12 
GeneralIf you really wanted to go hog wild, you could go this route... PinmemberAndrew Rissing4-Jan-12 8:15 
GeneralRe: It would work, and probably improve performance - but it wou... PinmvpOriginalGriff4-Jan-12 9:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 7 Jan 2012
Article Copyright 2012 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid