DefaultValueAttribute Class
Specifies the default value for a property.
For a list of all members of this type, see DefaultValueAttribute Members.
System.Object
System.Attribute
System.ComponentModel.DefaultValueAttribute
[Visual Basic] <AttributeUsage(AttributeTargets.All)> NotInheritable Public Class DefaultValueAttribute Inherits Attribute [C#] [AttributeUsage(AttributeTargets.All)] public sealed class DefaultValueAttribute : Attribute [C++] [AttributeUsage(AttributeTargets::All)] public __gc __sealed class DefaultValueAttribute : public Attribute [JScript] public AttributeUsage(AttributeTargets.All) 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.
Remarks
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.
Example
[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 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; } } [C++] private: bool myVal; public: [DefaultValue(false)] __property bool get_MyProperty() { return myVal; } __property void set_MyProperty( bool value ) { myVal=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 = _ 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()); [C++] // Gets the attributes for the property. AttributeCollection* attributes = TypeDescriptor::GetProperties(this)->Item[S"MyProperty"]->Attributes; /* Prints the default value by retrieving the DefaultValueAttribute * from the AttributeCollection. */ DefaultValueAttribute* myAttribute = dynamic_cast<DefaultValueAttribute*>(attributes->Item[__typeof(DefaultValueAttribute)]); 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
in the upper-left corner of the page.
Requirements
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