Click here to Skip to main content
12,503,198 members (61,972 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

80.4K views
11 bookmarked
Posted

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; }
[DefaultValue(240)]
public int MyInt { get; set; }
[DefaultValue(110)]
public int EnvelopeHeight { get; set; }
[DefaultValue(true)]
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]

License

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

Share

About the Author

OriginalGriff
CEO
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?

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
Hassen Doghmen12-Sep-14 3:09
memberHassen Doghmen12-Sep-14 3:09 
QuestionMessage Automatically Removed Pin
5-Jan-14 21:47
professionalRavi.Kumar0215-Jan-14 21:47 
GeneralMy vote of 1 Pin
cocowalla22-Jul-13 9:49
membercocowalla22-Jul-13 9:49 
GeneralMy vote of 1 Pin
vijayksingh20-Mar-13 21:35
membervijayksingh20-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
memberAndrew 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
memberAndrew 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
memberdojohansen13-Jan-12 3:51 
GeneralAll these "solutions" have no meaning just by one reason: yo... Pin
Member 308248711-Jan-12 20:33
memberMember 308248711-Jan-12 20:33 
GeneralReason for my vote of 5 Excellent thought. Though due to lac... Pin
All Time Programming11-Jan-12 18:44
memberAll Time Programming11-Jan-12 18:44 
GeneralReflection is sloooow! Reflection has many great uses. Setti... Pin
cathal_mchale9-Jan-12 21:45
membercathal_mchale9-Jan-12 21:45 
GeneralReason for my vote of 5 ok jkhj Pin
prakash.rath1239-Jan-12 20:40
memberprakash.rath1239-Jan-12 20:40 
GeneralReason for my vote of 5 Great example of "thinking out of th... Pin
BillWoodruff9-Jan-12 14:10
memberBillWoodruff9-Jan-12 14:10 
GeneralNeat! :) Pin
Luc Pattyn4-Jan-12 9:58
mvpLuc Pattyn4-Jan-12 9:58 
GeneralKewl! Pin
Collin Jasnoch4-Jan-12 8:12
memberCollin Jasnoch4-Jan-12 8:12 
GeneralIf you really wanted to go hog wild, you could go this route... Pin
Andrew Rissing4-Jan-12 7:15
memberAndrew Rissing4-Jan-12 7:15 
GeneralRe: It would work, and probably improve performance - but it wou... Pin
OriginalGriff4-Jan-12 8:03
mvpOriginalGriff4-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.

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