Export (0) Print
Expand All
Expand Minimize

Type.IsSubclassOf Method

Determines whether the class represented by the current Type derives from the class represented by the specified Type.

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

[ComVisibleAttribute(true)] 
public virtual bool IsSubclassOf (
	Type c
)
/** @attribute ComVisibleAttribute(true) */ 
public boolean IsSubclassOf (
	Type c
)
ComVisibleAttribute(true) 
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.

Exception typeCondition

ArgumentNullException

The c parameter is a null reference (Nothing in Visual Basic).

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

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.

NoteNote

If the 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 demonstrates the use of the IsSubclassOf method.

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));
    }
}

import System.*;

public interface IMyIfc
{
} //IMyIfc

public interface IDerived extends IMyIfc
{
} //IDerived

public class Class1 implements IMyIfc
{
} //Class1

public class MyDerivedClass extends Class1
{
} //MyDerivedClass

class IsSubclassTest
{
    public static void main(String[] args)
    {
        Type iMyIfcType = IMyIfc.class.ToType();
        Type iMyDerivedType = IDerived.class.ToType();
        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 = Array.class.ToType();

        Console.WriteLine("Is Array a derived class of int[]? {0}",
            System.Convert.ToString(arrayType.IsSubclassOf(arrayOfIntsType)));
        Console.WriteLine("Is int [] a derived class of Array? {0}", 
            System.Convert.ToString(arrayOfIntsType.IsSubclassOf(arrayType)));
        Console.WriteLine("Is IMyIfc a derived class of IDerived? {0}", 
            System.Convert.ToString(iMyIfcType.IsSubclassOf(iMyDerivedType)));
        Console.WriteLine("Is myclass a derived class of Class1? {0}", 
            System.Convert.ToString(mcType.IsSubclassOf(mcType)));
        Console.WriteLine("Is myderivedclass a derived class of Class1? {0}", 
            System.Convert.ToString(mdcType.IsSubclassOf(mcType)));
    } //main
} //IsSubclassTest

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft