This documentation is archived and is not being maintained.

Attribute::IsDefaultAttribute Method

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

virtual bool IsDefaultAttribute()

Return Value

Type: System::Boolean
true if this instance is the default attribute for the class; otherwise, false.

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, then returns a Boolean value that indicates whether the value of this instance is equal to the standard value. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.

The following code example illustrates the use of IsDefaultAttribute.

using namespace System;
using namespace System::Reflection;

// An enumeration of animals. Start at 1 (0 = uninitialized). 
public enum class Animal
   // Pets.
   Dog = 1,
   Cat, Bird

// A custom attribute to allow a target to have a pet. 
public ref class AnimalTypeAttribute: public Attribute

   // The constructor is called when the attribute is set.
   AnimalTypeAttribute( Animal pet )
      thePet = pet;

   // Provide a default constructor and make Dog the default.
      thePet = Animal::Dog;


   // Keep a variable internally ...
   Animal thePet;


   property Animal Pet 
      // .. and show a copy to the outside world.
      Animal get()
         return thePet;
      void set( Animal value )
         thePet = value;


   // Override IsDefaultAttribute to return the correct response. 
   virtual bool IsDefaultAttribute() override
      return thePet == Animal::Dog;

public ref class TestClass

   // Use the default constructor.

   void Method1(){}

int main()
   // Get the class type to access its metadata.
   Type^ clsType = TestClass::typeid;

   // Get type information for the method.
   MethodInfo^ mInfo = clsType->GetMethod( "Method1" );

   // Get the AnimalType attribute for the method.
   AnimalTypeAttribute^ atAttr = dynamic_cast<AnimalTypeAttribute^>(Attribute::GetCustomAttribute( mInfo, AnimalTypeAttribute::typeid ));

   // Check to see if the default attribute is applied.
   Console::WriteLine( "The attribute {0} for method {1} in class {2}", atAttr->Pet, mInfo->Name, clsType->Name );
   Console::WriteLine( "{0} the default for the AnimalType attribute.", atAttr->IsDefaultAttribute() ? (String^)"is" : "is not" );

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

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