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
Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. 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
[Visual Basic, 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; } }
[Visual Basic, 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#] 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++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
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 - Windows CE .NET
Assembly: System (in System.dll)
DefaultValueAttribute Members | System.ComponentModel Namespace | Attribute | PropertyDescriptor