Search code examples
c#.netpropertiesdefaultserializable

Set a default value to a property and make it serializable


I need to set a default value for a property, but I can't do it like this:

private int prop = 1;

public Prop
{
     get { return prop;} ...
}

Because I need to serialize this class, and if I do it, I loose the default value.

Are you aware of any solution that works after the serialization and before, adding an attribute to the property?

I'm working with c# with framework 3.5.


Solution

  • DefaultValueAttribute

    [DefaultValue("SomeValue")]
    public string Prop { get; set; }
    

    You can read a lot about serialization here: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx