Type.IsSubclassOf(Type) Method

Definition

Determines whether the current Type derives from the specified Type.

public virtual bool IsSubclassOf (Type c);
[System.Runtime.InteropServices.ComVisible(true)]
public virtual bool IsSubclassOf (Type c);

Parameters

c
Type

The type to compare with the current type.

Returns

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

Implements

Attributes

Exceptions

Examples

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

using System;

public class Class1 { }
public class DerivedC1 : Class1 { }

class IsSubclassTest
{
   public static void Main()
   {
      Console.WriteLine("DerivedC1 subclass of Class1: {0}",
                         typeof(DerivedC1).IsSubclassOf(typeof(Class1)));
   }
}
// The example displays the following output:
//        DerivedC1 subclass of Class1: True

Remarks

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 the IsAssignableFrom method for that purpose, as the following example shows.

using System;

public interface IInterface
{
   void Display();
}

public class Implementation : IInterface
{
   public void Display()
   {
      Console.WriteLine("The implementation...");
   }
}

public class Example
{
   public static void Main()
   {
      Console.WriteLine("Implementation is a subclass of IInterface:   {0}",
                        typeof(Implementation).IsSubclassOf(typeof(IInterface)));
      Console.WriteLine("IInterface is assignable from Implementation: {0}",
                        typeof(IInterface).IsAssignableFrom(typeof(Implementation)));
   }
}
// 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.

Note

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.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also