13

I got this code in a user control:

[DefaultValue(typeof(Color), "Red")]
public Color MyColor { get; set; }

How can I change MyColor to be its default value?

Samuel
  • 37,778
  • 11
  • 85
  • 87
Melursus
  • 10,328
  • 19
  • 69
  • 103
  • In VS2015 - [C# 6](https://blogs.msdn.microsoft.com/csharpfaq/2014/11/20/new-features-in-c-6/) feature: `public bool MyColor { get; set; } = Color.Red;` – marbel82 Nov 21 '16 at 08:51

6 Answers6

21

The DefaultValueAttribute does not set the property to the value, it is purely informational. The Visual Studio designer will display this value as non-bold and other values as bold (changed), but you'll still have to set the property to the value in the constructor.

The designer will generate code for the property if the value was set by the user, but you can remove that code by right clicking on the property and clicking Reset.

Samuel
  • 37,778
  • 11
  • 85
  • 87
OregonGhost
  • 23,359
  • 7
  • 71
  • 108
11

DefaultValueAttribute is not used by the compiler, and (perhaps confusingly) it doesn't set the initial value. You need to do this your self in the constructor. Places that do use DefaultValueAttribute include:

  • PropertyDescriptor - provides ShouldSerializeValue (used by PropertyGrid etc)
  • XmlSerializer / DataContractSerializer / etc (serialization frameworks) - for deciding whether it needs to be included

Instead, add a constructor:

public MyType() {
  MyColor = Color.Red;
}

(if it is a struct with a custom constructor, you need to call :base() first)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
7

It is informal, but you can use it via reflection, for example, place in your constructor the following:

foreach (PropertyInfo p in this.GetType().GetProperties())
{
    foreach (Attribute attr in p.GetCustomAttributes(true))
    {
        if (attr is DefaultValueAttribute)
        {
            DefaultValueAttribute dv = (DefaultValueAttribute)attr;
            p.SetValue(this, dv.Value);
        }
    }
}
gehho
  • 9,049
  • 3
  • 45
  • 59
nothrow
  • 15,882
  • 9
  • 57
  • 104
  • 1
    In the example given, the attribute is set against the property, not the field. – Marc Gravell Apr 01 '09 at 13:58
  • 1
    Ok, so - rewrite - foreach (FieldInfo f in this.GetType().GetFields()) as foreach (PropertyInfo f in this.GetType().GetProperties()) – nothrow Apr 01 '09 at 14:01
  • Wait wtf? Why is this accepted? If this answers your question, you need to rewrite your question. – Samuel Apr 01 '09 at 14:11
4

The "DefaultValue" attribute does not write code for you... but rather it is used for you to tell people (such as Mr Property Grid, or Mr Serializer Guy) that you plan to set the default value to Red.

This is useful for things like the PropertyGrid... as it will BOLD any color other than Red... also for serialization, people may choose to omit sending that value, because you informed them that it's the default :)

Timothy Khouri
  • 31,315
  • 21
  • 88
  • 128
2

I adapted the answer of Yossarian:

foreach (PropertyInfo f in this.GetType().GetProperties())
{
    foreach (Attribute attr in f.GetCustomAttributes(true))
    {
        if (attr is DefaultValueAttribute)
        {
            DefaultValueAttribute dv = (DefaultValueAttribute)attr;
            f.SetValue(this, dv.Value, null);
        }
    }
}
infabo
  • 158
  • 5
2

Are you initializing MyColor in your constructor?

The DefaultValue attribute does not actually set any values. It simply instructs the designer for which value to not generate code and will also show the default value non-bold to reflect this.

lc.
  • 113,939
  • 20
  • 158
  • 187