Type.IsSubclassOf Method (Type)

 

Determines whether the current Type derives from the specified Type.

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

<ComVisibleAttribute(True)>
Public Overridable Function IsSubclassOf (
	c As Type
) As Boolean

Parameters

c
Type: System.Type

The type to compare with the current type.

Return Value

Type: System.Boolean

true if the current Type derives from c; otherwise, false. This method also returns false if c and the current Type are equal.

Exception Condition
ArgumentNullException

c is null.

You can call the IsSubclassOf method to determine any of the following:

  • Whether one class derives from another.

  • Whether a type derives from ValueType. However, the IsValueType is a more efficient way to determine whether a type is a value type.

  • Whether a type derives from Enum. However, the IsEnum method is a more efficient way to determine whether a type is an enumeration.

  • Whether a type is a delegate, that is, whether it derives from either Delegate or MulticastDelegate.

The IsSubclassOf method cannot be used to determine whether an interface derives from another interface, or whether a class implements an interface. Use theIsAssignableFrom method for that purpose, as the following example shows.

Public Interface IInterface
   Sub Display()
End Interface

Public Class Implementation : Implements IInterface
   Public Sub Display() _
      Implements IInterface.Display

      Console.WriteLine("The implementation...")
   End Sub
End Class

Module Example
   Public Sub Main()
      Console.WriteLine("Implementation is a subclass of IInterface:   {0}",
                        GetType(Implementation).IsSubclassOf(GetType(IInterface)))
      Console.WriteLine("IInterface is assignable from Implementation: {0}",
                        GetType(IInterface).IsAssignableFrom(GetType(Implementation)))
   End Sub
End Module
' The example displays the following output:
'       Implementation is a subclass of IInterface:   False
'       IInterface is assignable from Implementation: True

If the current Type represents a type parameter in the definition of a generic type or generic method, it derives from its class constraint or from System.Object if it has no class constraint.

System_CAPS_noteNote

Except when used with interfaces, IsSubclassOf is the converse of IsAssignableFrom. That is, if t1.IsSubclassOf(t2) is true, then t2.IsAssignableFrom(t1) is also true.

This method can be overridden by a derived class.

The following example creates a class named Class1and a derived class named DerivedC1. It calls the IsSubclassOf method to show that DerivedC1 is a subclass of Class1.

Public Class Class1
End Class

Public Class DerivedC1 : Inherits Class1
End Class

Public Module Example
   Public Sub Main()
      Console.WriteLine("DerivedC1 subclass of Class1: {0}",
                         GetType(DerivedC1).IsSubClassOf(GetType(Class1)))
   End Sub
End Module
' The example displays the following output:
'       DerivedC1 subclass of Class1: True

.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: