This documentation is archived and is not being maintained.

DefaultValueAttribute Class

Specifies the default value for a property.

For a list of all members of this type, see DefaultValueAttribute Members.


[Visual Basic]
NotInheritable Public Class DefaultValueAttribute
   Inherits Attribute
public sealed class DefaultValueAttribute : Attribute
public __gc __sealed class DefaultValueAttribute : public
class DefaultValueAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


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.

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


[Visual Basic, C#, C++] 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
        Return MyVar
    End Get
        MyVar = Value
    End Set 
End Property


private bool myVal=false;

 public bool MyProperty {
    get {
       return myVal;
    set {


bool myVal;

 __property bool get_MyProperty() {
       return myVal;
 __property void set_MyProperty( bool value ) {

[Visual Basic, C#, C++] 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.

[Visual Basic, C#, C++] 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 = _

' 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()))

// Gets the attributes for the property.
 AttributeCollection attributes = 
 /* Prints the default value by retrieving the DefaultValueAttribute 
  * from the AttributeCollection. */
 DefaultValueAttribute myAttribute = 
 Console.WriteLine("The default value is: " + myAttribute.Value.ToString());

// Gets the attributes for the property.
 AttributeCollection* attributes = 
 /* Prints the default value by retrieving the DefaultValueAttribute 
  * from the AttributeCollection. */
 DefaultValueAttribute* myAttribute = 
 Console::WriteLine(S"The default value is: {0}", myAttribute->Value);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.ComponentModel

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System (in System.dll)

See Also

DefaultValueAttribute Members | System.ComponentModel Namespace | Attribute | PropertyDescriptor