Click here to Skip to main content
15,921,884 members
Articles / Programming Languages / C#

Setting a default value for C# Auto-implemented properties

Rate me:
Please Sign up or sign in to vote.
4.83/5 (14 votes)
7 Jan 2012CPOL 131.8K   11   16
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)

Written By
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?

Comments and Discussions

GeneralMy vote of 5 Pin
Hassen Doghmen12-Sep-14 3:09
Hassen Doghmen12-Sep-14 3:09 
GeneralMy vote of 1 Pin
cocowalla22-Jul-13 9:49
cocowalla22-Jul-13 9:49 
GeneralMy vote of 1 Pin
Singh Vijay Kumar20-Mar-13 21:35
professionalSingh Vijay Kumar20-Mar-13 21:35 
GeneralRe: I've posted the code to what I was describing below for your... Pin
Andrew Rissing5-Jan-12 3:53
Andrew Rissing5-Jan-12 3:53 
GeneralRe: It would just rely on what you described above to acquire wh... Pin
Andrew Rissing4-Jan-12 11:47
Andrew Rissing4-Jan-12 11:47 
GeneralReason for my vote of 3 I don't really agree this is missing... Pin
dojohansen13-Jan-12 3:51
dojohansen13-Jan-12 3:51 
GeneralAll these "solutions" have no meaning just by one reason: yo... Pin
Thornik11-Jan-12 20:33
Thornik11-Jan-12 20:33 
GeneralReason for my vote of 5 Excellent thought. Though due to lac... Pin
All Time Programming11-Jan-12 18:44
All Time Programming11-Jan-12 18:44 
GeneralReflection is sloooow! Reflection has many great uses. Setti... Pin
cathal_mchale9-Jan-12 21:45
cathal_mchale9-Jan-12 21:45 
GeneralReason for my vote of 5 ok jkhj Pin
prakash.rath1239-Jan-12 20:40
prakash.rath1239-Jan-12 20:40 
GeneralReason for my vote of 5 Great example of "thinking out of th... Pin
BillWoodruff9-Jan-12 14:10
professionalBillWoodruff9-Jan-12 14:10 
GeneralNeat! :) Pin
Luc Pattyn4-Jan-12 9:58
sitebuilderLuc Pattyn4-Jan-12 9:58 
GeneralMessage Removed Pin
4-Jan-12 8:12
professionalN_tro_P4-Jan-12 8:12 
GeneralIf you really wanted to go hog wild, you could go this route... Pin
Andrew Rissing4-Jan-12 7:15
Andrew Rissing4-Jan-12 7:15 
GeneralRe: It would work, and probably improve performance - but it wou... Pin
OriginalGriff4-Jan-12 8:03
mveOriginalGriff4-Jan-12 8:03 

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

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