Skip to main content
.NET Framework Class Library
AttributeIsDefaultAttribute 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)
Syntax
Public Overridable Function IsDefaultAttribute As [%$TOPIC/tbkb5x6t_en-us_VS_110_1_0_0_0_0%]
public virtual [%$TOPIC/tbkb5x6t_en-us_VS_110_1_0_1_0_0%] IsDefaultAttribute()
public:
virtual [%$TOPIC/tbkb5x6t_en-us_VS_110_1_0_2_0_0%] IsDefaultAttribute()
abstract IsDefaultAttribute : unit -> [%$TOPIC/tbkb5x6t_en-us_VS_110_1_0_3_0_0%]  
override IsDefaultAttribute : unit -> [%$TOPIC/tbkb5x6t_en-us_VS_110_1_0_3_0_1%]

Return Value

Type: SystemBoolean
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, 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.

Examples

The following code example illustrates the use of IsDefaultAttribute.

Imports System
Imports System.Reflection

Module DemoModule

    ' An enumeration of animals. Start at 1 (0 = uninitialized). 
    Enum Animal
        ' Pets
        Dog = 1
        Cat
        Bird
    End Enum 

    ' Visual Basic requires that the AttributeUsage be specified. 
    ' A custom attribute to allow a target to have a pet.
    <AttributeUsage(AttributeTargets.Method)> _
    Public Class AnimalTypeAttribute
        Inherits Attribute

        ' The constructor is called when the attribute is set. 
        Public Sub New(ByVal animal As Animal)
            Me.thePet = animal
        End Sub 

        ' Provide a default constructor and make Dog the default. 
        Public Sub New()
            thePet = Animal.Dog
        End Sub 

        ' Keep a variable internally ... 
        Protected thePet As Animal

        ' .. and show a copy to the outside world. 
        Public Property Pet() As Animal
            Get 
                Return thePet
            End Get 
            Set(ByVal Value As Animal)
                thePet = Value
            End Set 
        End Property 

        ' Override IsDefaultAttribute to return the correct response. 
        Public Overrides Function IsDefaultAttribute() As Boolean 
            If thePet = Animal.Dog Then 
                Return True 
            Else 
                Return False 
            End If 
        End Function 

    End Class 

    Public Class TestClass
        ' Use the default constructor.
        <AnimalType()> _
        Public Sub Method1()
        End Sub 

    End Class 

    Sub Main()
        ' Get the class type to access its metadata. 
        Dim clsType As Type = GetType(TestClass)
        ' Get type information for the method. 
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' Get the AnimalType attribute for the method. 
        Dim attr As Attribute = Attribute.GetCustomAttribute(mInfo, _
            GetType(AnimalTypeAttribute))
        If Not attr Is Nothing And TypeOf attr Is AnimalTypeAttribute Then 
            ' Convert the attribute to the required type. 
            Dim atAttr As AnimalTypeAttribute = _
                CType(attr, AnimalTypeAttribute)
            Dim strDef As String 
            ' Check to see if the default attribute is applied. 
            If atAttr.IsDefaultAttribute() Then
                strDef = "is" 
            Else
                strDef = "is not" 
            End If 
            ' Display the result.
            Console.WriteLine("The attribute {0} for method {1} " & _
                    "in class {2}", atAttr.Pet.ToString(), mInfo.Name, _
                    clsType.Name)
            Console.WriteLine("{0} the default for the AnimalType " & _
                    "attribute.", strDef)
        End If 
    End Sub 
End Module
using System;
using System.Reflection;

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

    // A custom attribute to allow a target to have a pet. 
    public class AnimalTypeAttribute : Attribute 
    {
        // The constructor is called when the attribute is set. 
        public AnimalTypeAttribute(Animal pet) 
        {
            thePet = pet;
        }

        // Provide a default constructor and make Dog the default. 
        public AnimalTypeAttribute() 
        {
            thePet = Animal.Dog;
        }

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

        // .. and show a copy to the outside world. 
        public Animal Pet 
        {
            get { return thePet; }
            set { thePet = Pet; }
        }

        // Override IsDefaultAttribute to return the correct response. 
        public override bool IsDefaultAttribute() 
        {
            if (thePet == Animal.Dog)
                return true;

            return false;
        }
    }

    public class TestClass 
    {
        // Use the default constructor.
        [AnimalType]
        public void Method1()
        {}
    }

    class DemoClass 
    {
        static void Main(string[] args) 
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get type information for the method.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // Get the AnimalType attribute for the method.
            AnimalTypeAttribute atAttr = 
                (AnimalTypeAttribute)Attribute.GetCustomAttribute(mInfo,
                typeof(AnimalTypeAttribute));
            // 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() ? "is" : "is not");
        }
    }
}
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
{
public:

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

   // Provide a default constructor and make Dog the default.
   AnimalTypeAttribute()
   {
      thePet = Animal::Dog;
   }

protected:

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

public:

   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
{
public:

   // Use the default constructor.

   [AnimalType]
   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" );
}
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.