C# Type IsSubclassOf

Description

Type IsSubclassOf determines whether the class represented by the current Type derives from the class represented by the specified Type.

Syntax

Type.IsSubclassOf has the following syntax.


[ComVisibleAttribute(true)]
public virtual bool IsSubclassOf(
  Type c
)

Parameters

Type.IsSubclassOf has the following parameters.

  • c - The type to compare with the current type.

Returns

Type.IsSubclassOf method returns 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.

Example

The following example demonstrates the use of the IsSubclassOf method by creating an instance of a class and an instance of its derived class.


/*from www. j a  va2  s  .c o  m*/
using System;

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

class IsSubclassTest
{
    public static void Main()
    {
        Class1 myClass = new Class1();
        DerivedC1 myDClass = new DerivedC1();
        Type myClassType = myClass.GetType();
        Type myDClassType = myDClass.GetType();

        Console.WriteLine(myDClassType.IsSubclassOf(myClassType));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version