DefaultValueAttribute Class
.NET Framework Class Library
DefaultValueAttribute Class

Updated: September 2008

Specifies the default value for a property.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)
Visual Basic (Declaration)
<AttributeUsageAttribute(AttributeTargets.All)> _
Public Class DefaultValueAttribute _
    Inherits Attribute
Visual Basic (Usage)
Dim instance As DefaultValueAttribute
C#
[AttributeUsageAttribute(AttributeTargets.All)]
public class DefaultValueAttribute : Attribute
Visual C++
[AttributeUsageAttribute(AttributeTargets::All)]
public ref class DefaultValueAttribute : public Attribute
JScript
public class DefaultValueAttribute extends Attribute

You can create a DefaultValueAttribute with any value. A member's default value is typically its initial value. A visual designer can use the default value to reset the member's value. Code generators can use the default values also to determine whether code should be generated for the member.

NoteNote:

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.

For more information, see Attributes Overview and Extending Metadata Using Attributes.

TopicLocation
Developing Custom Data-Bound Controls for ASP.NET 1.1Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 1.1Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Developing Custom Data-Bound Controls for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Developing Custom Data-Bound Controls for ASP.NET 2.0Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 2.0Authoring ASP.NET Controls
Developing Custom Data-Bound Controls for ASP.NET 2.0 and laterBuilding ASP .NET Web Applications in Visual Studio
Developing Custom Data-Bound Controls for ASP.NET 2.0 and laterBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web ControlBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 1.1Building ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0Authoring ASP.NET Controls
Walkthrough: Creating a Custom Data-Bound ASP.NET Web Control for ASP.NET 2.0Building ASP .NET Web Applications in Visual Studio
Walkthrough: Developing and Using a Custom Server ControlAuthoring ASP.NET Controls
Walkthrough: Developing and Using a Custom Server ControlAuthoring ASP.NET Controls
Walkthrough: Developing and Using a Custom Server ControlBuilding Applications with Visual Web Developer
Walkthrough: Developing and Using a Custom Server ControlBuilding ASP .NET Web Applications in Visual Studio

The following example sets the default value of MyProperty to false.

Visual Basic
Private MyVar as Boolean = False
<DefaultValue(False)> _
Public Property MyProperty() As Boolean
    Get
        Return MyVar
    End Get
    Set
        MyVar = Value
    End Set 
End Property

C#
private bool myVal=false;

[DefaultValue(false)]
 public bool MyProperty {
    get {
       return myVal;
    }
    set {
       myVal=value;
    }
 }
Visual C++
private:
   bool myVal;

public:
   [DefaultValue(false)]
   property bool MyProperty 
   {
      bool get()
      {
         return myVal;
      }

      void set( bool value )
      {
         myVal = value;
      }
   }

The next example checks the default value of MyProperty. First the code gets a PropertyDescriptorCollection with all the properties for the object. Next it indexes into the PropertyDescriptorCollection to get MyProperty. Then it returns the attributes for this property and saves them in the attributes variable.

The example then prints the default value by retrieving the DefaultValueAttribute from the AttributeCollection, and writing its name to the console screen.

Visual Basic
' Gets the attributes for the property.
Dim attributes As AttributeCollection = _
    TypeDescriptor.GetProperties(Me)("MyProperty").Attributes

' Prints the default value by retrieving the DefaultValueAttribute
' from the AttributeCollection. 
Dim myAttribute As DefaultValueAttribute = _
    CType(attributes(GetType(DefaultValueAttribute)), DefaultValueAttribute)
Console.WriteLine(("The default value is: " & myAttribute.Value.ToString()))
C#
// Gets the attributes for the property.
 AttributeCollection attributes = 
    TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;

 /* Prints the default value by retrieving the DefaultValueAttribute 
  * from the AttributeCollection. */
 DefaultValueAttribute myAttribute = 
    (DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
 Console.WriteLine("The default value is: " + myAttribute.Value.ToString());
Visual C++
// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "MyProperty" ]->Attributes;

/* Prints the default value by retrieving the DefaultValueAttribute 
      * from the AttributeCollection. */
DefaultValueAttribute^ myAttribute = dynamic_cast<DefaultValueAttribute^>(attributes[ DefaultValueAttribute::typeid ]);
Console::WriteLine( "The default value is: {0}", myAttribute->Value );
System..::.Object
  System..::.Attribute
    System.ComponentModel..::.DefaultValueAttribute
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

September 2008

Added note explaining that adding an attribute does not automatically initialize the member.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
default size of a user control      mharab   |   Edit   |   Show History
Hello

I have a UserControl sized to 100; 200
When I drop it in my form the initial size is 100;200 (as I prefer). Then I decide to change the size of my UserControl to 200;200 but although I have not set any value to the size of the dropped control in my form, recompiling the project does not affect the new size of the user control in my form.

Surprisingly if I reset the Size property of my user control in the form, the size changes to 150; 150! It seems some how, there is a DefaultValue for Size property.

How can I solve my problem? I want when I resize my UserControl in its designer, it affects all places it is used.

Thanks in advance.
Hamed
Tags What's this?: Add a tag
Flag as ContentBug
Default value for Size      Voytas   |   Edit   |   Show History
Hamed,

Here is the solution: [DefaultValue(typeof(Size), "100, 200")]

Regards.
Tags What's this?: Add a tag
Flag as ContentBug
confusing and sample code implies the wrong behavior      ricka0   |   Edit   |   Show History
This sample is so confusing it has a KB explaining the real meaning. See http://support.microsoft.com/kb/311339
Provide a complete sample showing how to use this. Doc states "Specifies the default value for a property." which is incorrect - it specifies the default Attribute value for the property - not the property. You need to actively use the attribute value.
Clarification      idontcareidontcare   |   Edit   |   Show History

If you are searching for how to specify the default value for a property on your own custom control so that it appears in the control's designer properties do not use this attribute; the default value when your control is dropped on a form is always 0 or false irrespective of any [DefaultValue(..)] you may have applied.

Perhaps this help page could have a link added to what is the appropriate attribute as I can't find it.

Tags What's this?: Add a tag
Flag as ContentBug
Property name is a total misnomer      Chris Lively   |   Edit   |   Show History
Please rename this to something like "NotReallyADefaultValueDefaultValue" or something equally arcane that really says what it does. It's obviously not a default value from the control developer's perspective, nor from the control user's perspective. It seems this is only a default value from some little menu options point of view. So, how about naming it "TheDefaultValueSetByAMenuOptionThatNoOneUsesSoDontBotherDefaultValue" or "DefaultValueThatWillWasteYourTimeTryingToFigureOutThatItsReallyNotADefaultValue"
Tags What's this?: Add a tag
Flag as ContentBug
Processing
Page view tracker