System.ComponentModel.DefaultValueAttribute Class

Assembly: System.dll
Namespace: System.ComponentModel
Summary
Specifies the default value for a property.
C# Syntax:
[AttributeUsage(AttributeTargets.All)]
public sealed class DefaultValueAttribute : Attribute
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 the conceptual topic at MSDN: attributesoverview and the conceptual topic at MSDN: extendingmetadatausingattributes.

Example
The following example sets the default value of MyProperty to false.
[DefaultValue(false)]
 public bool MyProperty {
    get {
       // Insert code here.
       return true;
    }
    set {
       // Insert code here.
    }
 }

    

The next example checks the default value of MyProperty . First the code gets a System.ComponentModel.PropertyDescriptorCollection (not supported on the shared source CLI) with all the properties for the object. Next it indexes into the System.ComponentModel.PropertyDescriptorCollection (not supported on the shared source CLI) 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 System.ComponentModel.AttributeCollection (not supported on the shared source CLI) , and writing its name to the console screen.

    // 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());

    
See also:
System.ComponentModel Namespace | Attribute | System.ComponentModel.PropertyDescriptor (not supported on the shared source CLI)

System.ComponentModel.DefaultValueAttribute Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(bool value)

Initializes a new instance of the DefaultValueAttribute class using a Boolean value.
ctor #2 Overloaded:
.ctor(byte value)

Initializes a new instance of the DefaultValueAttribute class using an 8-bit unsigned integer.
ctor #3 Overloaded:
.ctor(char value)

Initializes a new instance of the DefaultValueAttribute class using a Unicode character.
ctor #4 Overloaded:
.ctor(double value)

Initializes a new instance of the DefaultValueAttribute class using a double-precision floating point number.
ctor #5 Overloaded:
.ctor(short value)

Initializes a new instance of the DefaultValueAttribute class using a 16-bit signed integer.
ctor #6 Overloaded:
.ctor(int value)

Initializes a new instance of the DefaultValueAttribute class using a 32-bit signed integer.
ctor #7 Overloaded:
.ctor(long value)

Initializes a new instance of the DefaultValueAttribute class using a 64-bit signed integer.
ctor #8 Overloaded:
.ctor(object value)

Initializes a new instance of the DefaultValueAttribute class.
ctor #9 Overloaded:
.ctor(float value)

Initializes a new instance of the DefaultValueAttribute class using a single-precision floating point number.
ctor #10 Overloaded:
.ctor(string value)

Initializes a new instance of the DefaultValueAttribute class using a String.
Public Properties
TypeId
(inherited from System.Attribute)
Read-only

See base class member description: System.Attribute.TypeId


When implemented in a derived class, gets a unique identifier for this Attribute.
Value Read-only

Gets the default value of the property this attribute is bound to.
Public Methods
Equals Overridden:
Returns whether the value of the given object is equal to the current DefaultValueAttribute.
GetHashCode Overridden:
Supports the Shared Source CLI infrastructure and is not intended to be used directly from your code
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
IsDefaultAttribute
(inherited from System.Attribute)
See base class member description: System.Attribute.IsDefaultAttribute


When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class.
Match
(inherited from System.Attribute)
See base class member description: System.Attribute.Match


When overridden in a derived class, returns a value indicating whether this instance equals a specified object.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.ComponentModel.DefaultValueAttribute Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the DefaultValueAttribute class using a Boolean value.
C# Syntax:
public DefaultValueAttribute(
   bool value
);
Parameters:

value

A Boolean that is the default value.

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the DefaultValueAttribute class using an 8-bit unsigned integer.
C# Syntax:
public DefaultValueAttribute(
   byte value
);
Parameters:

value

An 8-bit unsigned integer that is the default value.

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the DefaultValueAttribute class using a Unicode character.
C# Syntax:
public DefaultValueAttribute(
   char value
);
Parameters:

value

A Unicode character that is the default value.

Return to top


Overloaded ctor #4
Summary
Initializes a new instance of the DefaultValueAttribute class using a double-precision floating point number.
C# Syntax:
public DefaultValueAttribute(
   double value
);
Parameters:

value

A double-precision floating point number that is the default value.

Return to top


Overloaded ctor #5
Summary
Initializes a new instance of the DefaultValueAttribute class using a 16-bit signed integer.
C# Syntax:
public DefaultValueAttribute(
   short value
);
Parameters:

value

A 16-bit signed integer that is the default value.

Return to top


Overloaded ctor #6
Summary
Initializes a new instance of the DefaultValueAttribute class using a 32-bit signed integer.
C# Syntax:
public DefaultValueAttribute(
   int value
);
Parameters:

value

A 32-bit signed integer that is the default value.

Return to top


Overloaded ctor #7
Summary
Initializes a new instance of the DefaultValueAttribute class using a 64-bit signed integer.
C# Syntax:
public DefaultValueAttribute(
   long value
);
Parameters:

value

A 64-bit signed integer that is the default value.

Return to top


Overloaded ctor #8
Summary
Initializes a new instance of the DefaultValueAttribute class.
C# Syntax:
public DefaultValueAttribute(
   object value
);
Parameters:

value

An Object that represents the default value.

Return to top


Overloaded ctor #9
Summary
Initializes a new instance of the DefaultValueAttribute class using a single-precision floating point number.
C# Syntax:
public DefaultValueAttribute(
   float value
);
Parameters:

value

A single-precision floating point number that is the default value.

Return to top


Overloaded ctor #10
Summary
Initializes a new instance of the DefaultValueAttribute class using a String.
C# Syntax:
public DefaultValueAttribute(
   string value
);
Parameters:

value

A String that is the default value.

Return to top


Property: TypeId (read-only)
Inherited
See base class member description: System.Attribute.TypeId

Summary
When implemented in a derived class, gets a unique identifier for this Attribute.
C# Syntax:
public virtual object TypeId {get;}
Remarks
As implemented, this identifier is merely the Type of the attribute. However, it is intended that the unique identifier be used to identify two attributes of the same type.

Return to top


Property: Value (read-only)
Summary
Gets the default value of the property this attribute is bound to.
C# Syntax:
public object Value {get;}

Return to top


Overridden Method: Equals(
   object obj
)
Summary
Returns whether the value of the given object is equal to the current DefaultValueAttribute.
C# Syntax:
public override bool Equals(
   object obj
);
Parameters:

obj

The object to test the value equality of.

Return Value:
true if the value of the given object is equal to that of the current; otherwise, false.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~DefaultValueAttribute();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: GetHashCode()
Summary
Supports the Shared Source CLI infrastructure and is not intended to be used directly from your code
C# Syntax:
public override int GetHashCode();

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: IsDefaultAttribute()
Inherited
See base class member description: System.Attribute.IsDefaultAttribute

Summary
When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class.
C# Syntax:
public virtual bool IsDefaultAttribute();
Return Value:
true if this instance is the default attribute for the class; otherwise, false.
Remarks
The default implementation of this class returns false, and must be implemented in the derived class to be useful to that class.

The implementation of this method in a derived class compares the value of this instance to a standard, default value obtained by some means, then returns a Boolean value that indicates whether the value of this instance is equal to the standard. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.

Return to top


Method: Match(
   object obj
)
Inherited
See base class member description: System.Attribute.Match

Summary
When overridden in a derived class, returns a value indicating whether this instance equals a specified object.
C# Syntax:
public virtual bool Match(
   object obj
);
Parameters:

obj

An Object to compare with this instance of Attribute.

Return Value:
true if this instance equals obj; otherwise, false.
Remarks
This method determines if one Attribute equals another. Its default implementation is the same as Attribute.Equals, which performs a value and reference comparison. Override this method to implement support for attribute values, such as flags or bitfields, that consist of components that are meaningful in themselves. For example, consider an attribute whose value is a binary field divided into a bitfield of flags. Two instances of this attribute have one flag in set in common while all the other flags differ. The Equal method cannot determine that the two instances have the same flag set, but the Match method can.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.