Click here to Skip to main content
15,891,607 members
Articles / Programming Languages / C#
Article

Setting Default Values on Automatic Properties

Rate me:
Please Sign up or sign in to vote.
3.53/5 (8 votes)
14 May 2008CPOL2 min read 95.4K   197   23   36
An article on implementing default values on Automatic Properties

Introduction

C# 3.0 introduces a great new feature called Automatic Properties, and if you haven’t already read about them, I would encourage you to read Scott Guthrie's introductory post.

As great as they are and as much time as they save, Automatic Properties have a serious drawback – you can’t set the default value of the property. Instead, the compiler will initialize value properties to 0, reference properties to null, and enum’s to the first member, and while this might work for some applications, it wasn’t working for mine.

Background

When I thought about the implementation, two options became apparent. One, I could create a base object class and have all of my classes inherit from this base class. This however isn’t a great solution because a number of my classes inherit from other classes outside of my control, and since .NET does not support multiple inheritances, it was clear this wasn’t going to work. To my rescue was the also new C# 3.0 feature, Extension Methods. If you haven’t already heard about Extension Methods, I’d recommend reading another one of Scott Guthrie's blog posts about them.

Using the Code

Using the code requires that you decorate your properties with an attribute already available in the System.ComponentModel namespace – if you haven’t already guessed it, it’s the aptly named DefaultValueAttribute attribute. As well, it requires a quick call to the InitDefaults() extension method from the constructor which I will discuss a bit later.

The attached code supplies a demo implementation of the TestObject and TestObjectInherited classes:

C#
public class TestObject
    {
        public TestObject()
        {
            this.InitDefaults();
        }

        [DefaultValue(-45)]
        public int DefaultInt
        {
            get;
            set;
        }

        [DefaultValue(10.23)]
        public double DefaultDouble
        {
            get;
            set;
        }

        [DefaultValue(true)]
        public bool DefaultBool
        {
            get;
            set;
        }

        [DefaultValue(TestEnum.Value2)]
        public TestEnum DefaultEnum
        {
            get;
            set;
        }

        [DefaultValue("DefaultString!")]
        public string DefaultString
        {
            get;
            set;
        }

        public string StringWithoutDefault
        {
            get;
            set;
        }

        public string ValueOfPrivateProperty
        {
            get
            {
                return PrivateProperty;
            }
        }

        [DefaultValue("This is a private property!")]
        protected string PrivateProperty
        {
            get;
            set;        
        }
    }

The magical InitDefaults() method is implemented as an extension method which uses reflection to set the value of the properties to the default value:

C#
public static void InitDefaults(this object o)
        {
            PropertyInfo[] props = o.GetType().GetProperties(BindingFlags.Public | 
                BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

            for (int i = 0; i < props.Length; i++)
            {
                PropertyInfo prop = props[i];                

                if (prop.GetCustomAttributes(true).Length > 0)
                {
                    object[] defaultValueAttribute = 
                        prop.GetCustomAttributes(typeof(DefaultValueAttribute), true);

                    if (defaultValueAttribute != null)                    
                    {
                        DefaultValueAttribute dva = 
                            defaultValueAttribute[0] as DefaultValueAttribute;
                        
                        if(dva != null)
                            prop.SetValue(o, dva.Value, null);                                
                    }
                }
            }
        }

Points of Interest

I decided to support initializing the default value of properties in inherited classes, but if you don’t want this behavior, you can simply pass false to GetCustomAttributes().

C#
if (prop.GetCustomAttributes(false).Length > 0)
                {
                    object[] defaultValueAttribute = prop.GetCustomAttributes
                        (typeof(DefaultValueAttribute), false);

                    if (defaultValueAttribute != null)                    
                    {
                        DefaultValueAttribute dva = 
                            defaultValueAttribute[0] as DefaultValueAttribute;
                        
                        if(dva != null)
                            prop.SetValue(o, dva.Value, null);                                
                    }
                }

History

  • 14th May, 2008: Initial post

License

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


Written By
Web Developer
Canada Canada
Steven is VP Development at MBC Computer Solutions Ltd. (http://www.mbccs.com), a Richmond Hill based company specializing in e-Business Application Development, e-Store Solutions, Managed Co-Location and Proactive IT services.

Steven has over 10 years experience in software and hardware design and is experienced with a large array of platforms, technologies and languages.

In his spare time, Steven enjoys a wide array of music, is an avid skier and enjoys spending time with friends.

Steven is the primary contributor of MBC's blog which can be read at http://blogs.mbccs.com/mbccomputersolutions

Comments and Discussions

 
GeneralRe: I Don't See The Value In Automatic Properties Pin
PIEBALDconsult15-May-08 9:29
mvePIEBALDconsult15-May-08 9:29 
GeneralRe: I Don't See The Value In Automatic Properties Pin
William E. Kempf15-May-08 9:42
William E. Kempf15-May-08 9:42 
GeneralRe: I Don't See The Value In Automatic Properties Pin
Steven Berkovitz15-May-08 9:44
Steven Berkovitz15-May-08 9:44 
GeneralRe: I Don't See The Value In Automatic Properties Pin
William E. Kempf15-May-08 9:46
William E. Kempf15-May-08 9:46 
GeneralRe: I Don't See The Value In Automatic Properties Pin
PIEBALDconsult15-May-08 9:49
mvePIEBALDconsult15-May-08 9:49 
GeneralRe: I Don't See The Value In Automatic Properties Pin
Steven Berkovitz15-May-08 9:50
Steven Berkovitz15-May-08 9:50 
GeneralRe: I Don't See The Value In Automatic Properties Pin
PIEBALDconsult15-May-08 10:11
mvePIEBALDconsult15-May-08 10:11 
GeneralRe: I Don't See The Value In Automatic Properties Pin
William E. Kempf15-May-08 10:44
William E. Kempf15-May-08 10:44 
PIEBALDconsult wrote:
Hmmm... I think I see what you're getting at there, but if you can alter the implementation you should, otherwise I think you're abusing the intent of extension methods.
It sounds like the proper thing to do would be to have an abstract class (though single inheritence may interfere).


If an abstract base class would work in the .NET world, we'd never have interfaces.

PIEBALDconsult wrote:
No, nothing else does.


Uhmm... sure they do. Try to use List<T> with out adding the appropriate using statement (and adding the appropriate reference). Extension methods aren't any different here.

PIEBALDconsult wrote:
I read lots of code on here that I'm not maintaining; I still need to understand it.


Foo foo = new Foo();
foo.Bar();


How does knowing whether or not Bar() is an extension method or not make the above code any more understandable? Let's get more concrete with an IoC library.

ObjectContainer container = new ObjectContainer();
container.RegisterType<IFoo, Foo>();
IFoo foo = container.Resolve<IFoo>();


Why would you need to know whether or not RegisterType() and Resolve() are extension methods? When reading code, how are you hindered from understanding the code? Seriously, I hear the complaint, but I don't hear any concrete explanation for it.

PIEBALDconsult wrote:
William E. Kempf wrote:
The non-extension method will be called.


Which may break things unexpectedly.


As may a million other code examples that you have no problems with. Just one example:

void Foo<t>(T bar) { /* do something */ }
void Foo(object bar) { /* do something */ }</t>


Which one will get called under what circumstances?

In both scenarios, the rules are well defined. The results aren't random. A developer without enough knowledge of the code base (and I'm not suggesting he need understand the entire code base) can certainly make a mistake in either scenario. This is readily detected and easily rectified.

William E. Kempf

GeneralRe: I Don't See The Value In Automatic Properties Pin
PIEBALDconsult15-May-08 12:03
mvePIEBALDconsult15-May-08 12:03 
GeneralRe: I Don't See The Value In Automatic Properties Pin
William E. Kempf16-May-08 2:47
William E. Kempf16-May-08 2:47 
GeneralRe: I Don't See The Value In Automatic Properties Pin
PIEBALDconsult16-May-08 10:42
mvePIEBALDconsult16-May-08 10:42 
GeneralRe: I Don't See The Value In Automatic Properties Pin
William E. Kempf16-May-08 14:59
William E. Kempf16-May-08 14:59 
GeneralRe: I Don't See The Value In Automatic Properties Pin
PIEBALDconsult16-May-08 19:30
mvePIEBALDconsult16-May-08 19:30 
GeneralRe: I Don't See The Value In Automatic Properties Pin
PIEBALDconsult15-May-08 14:22
mvePIEBALDconsult15-May-08 14:22 
GeneralRe: I Don't See The Value In Automatic Properties Pin
William E. Kempf16-May-08 2:49
William E. Kempf16-May-08 2:49 
GeneralRe: I Don't See The Value In Automatic Properties Pin
Polymorpher21-May-08 12:39
Polymorpher21-May-08 12:39 

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.