Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Type.IsSubclassOf Method

Determines whether the current Type derives from the specified Type.

[Visual Basic]
Public Overridable Function IsSubclassOf( _
   ByVal c As Type _
) As Boolean
[C#]
public virtual bool IsSubclassOf(
 Type c
);
[C++]
public: virtual bool IsSubclassOf(
 Type* c
);
[JScript]
public function IsSubclassOf(
   c : Type
) : Boolean;

Parameters

c
The Type to compare with the current Type.

Return Value

true if the Type represented by the c parameter and the current Type represent classes, and the class represented by the current Type derives from the class represented by c; otherwise, false. This method also returns false if c and the current Type represent the same class.

Exceptions

Exception Type Condition
ArgumentNullException The c parameter is a null reference (Nothing in Visual Basic).

Remarks

This method can be overridden by a derived class.

Example

[Visual Basic, C#, C++] The following example demonstrates the use of the IsSubclassOf method.

[Visual Basic] 
Imports System

Public Interface IMyIfc
End Interface 'IMyIfc

Public Interface IDerived
    Inherits IMyIfc
End Interface 'IDerived

Public Class Class1
    Implements IMyIfc
End Class 'Class1

Public Class MyDerivedClass
    Inherits Class1
End Class 'MyDerivedClass

Class IsSubclassTest

    Public Shared Sub Main()
        Dim imyifcType As Type = GetType(IMyIfc)
        Dim imyderivedType As Type = GetType(IDerived)
        Dim mc As New Class1()
        Dim mcType As Type = mc.GetType()
        Dim mdc = New MyDerivedClass()
        Dim mdcType As Type = mdc.GetType()
        Dim array(10) As Integer
        Dim arrayOfIntsType As Type = array.GetType()
        Dim arrayType As Type = GetType(Array)

        Console.WriteLine("Is Array a derived class of int[]? {0}", arrayType.IsSubclassOf(arrayOfIntsType))
        Console.WriteLine("Is int [] a derived class of Array? {0}", arrayOfIntsType.IsSubclassOf(arrayType))
        Console.WriteLine("Is IMyIfc a derived class of IDerived? {0}", imyifcType.IsSubclassOf(imyderivedType))
        Console.WriteLine("Is myclass a derived class of Class1? {0}", mcType.IsSubclassOf(mcType))
        Console.WriteLine("Is myderivedclass a derived class of Class1? {0}", mdcType.IsSubclassOf(mcType))
    End Sub 'Main
End Class 'IsSubclassTest

[C#] 
using System;
public interface IMyIfc {}
public interface IDerived : IMyIfc {}
public class Class1 : IMyIfc {}
public class MyDerivedClass : Class1 {}
class IsSubclassTest 
{
    public static void Main() 
    {
        Type imyifcType = typeof(IMyIfc);
        Type imyderivedType = typeof(IDerived);
        Class1 mc = new Class1();
        Type mcType = mc.GetType();
        Class1 mdc = new MyDerivedClass();
        Type mdcType = mdc.GetType();
        int [] array  = new int [10];
        Type arrayOfIntsType = array.GetType();
        Type arrayType = typeof(Array);
    
        Console.WriteLine("Is Array a derived class of int[]? {0}", arrayType.IsSubclassOf(arrayOfIntsType));
        Console.WriteLine("Is int [] a derived class of Array? {0}", arrayOfIntsType.IsSubclassOf(arrayType));
        Console.WriteLine("Is IMyIfc a derived class of IDerived? {0}", imyifcType.IsSubclassOf(imyderivedType));
        Console.WriteLine("Is myclass a derived class of Class1? {0}", mcType.IsSubclassOf(mcType));
        Console.WriteLine("Is myderivedclass a derived class of Class1? {0}", mdcType.IsSubclassOf(mcType));
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
public __gc __interface IMyIfc {};
public __gc __interface IDerived : IMyIfc {};
public __gc class Class1 : public IMyIfc {};
public __gc class MyDerivedClass : public Class1 {};

int main() {
   Type*  imyifcType = __typeof(IMyIfc);
   Type*  imyderivedType = __typeof(IDerived);
   Class1* mc = new Class1();
   Type*  mcType = mc->GetType();
   Class1* mdc = new MyDerivedClass();
   Type*  mdcType = mdc->GetType();
   Int32 array[] = new Int32[10];
   Type*  arrayOfIntsType = array->GetType();
   Type*  arrayType = __typeof(Array);

   Console::WriteLine(S"Is Array a derived class of Int32[]? {0}", arrayType->IsSubclassOf(arrayOfIntsType).ToString());
   Console::WriteLine(S"Is Int32[] a derived class of Array? {0}", arrayOfIntsType->IsSubclassOf(arrayType).ToString());
   Console::WriteLine(S"Is IMyIfc a derived class of IDerived? {0}", imyifcType->IsSubclassOf(imyderivedType).ToString());
   Console::WriteLine(S"Is myclass a derived class of Class1? {0}", mcType->IsSubclassOf(mcType).ToString());
   Console::WriteLine(S"Is myderivedclass a derived class of Class1? {0}", mdcType->IsSubclassOf(mcType).ToString());
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

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, Common Language Infrastructure (CLI) Standard

See Also

Type Class | Type Members | System Namespace | BaseType

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.