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

Type.BaseType Property

Gets the type from which the current Type directly inherits.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public abstract Type BaseType { get; }

Property Value

Type: System.Type
The Type from which the current Type directly inherits, or null if the current Type represents the Object class or an interface.

Implements

_Type.BaseType

The base type is the type from which the current type directly inherits. Object is the only type that does not have a base type, therefore null is returned as the base type of Object.

Interfaces inherit from zero or more base interfaces; therefore, this property returns null if the Type object represents an interface. The base interfaces can be determined with GetInterfaces or FindInterfaces.

If the current Type represents a constructed generic type, the base type reflects the generic arguments. For example, consider the following declarations:

class B<U> { }
class C<T> : B<T> { }

For the constructed type C<int> (C(Of Integer) in Visual Basic), the BaseType property returns B<int>.

If the current Type represents a type parameter of a generic type definition, BaseType returns the class constraint, that is, the class the type parameter must inherit. If there is no class constraint, BaseType returns System.Object.

This property is read-only.

The following example demonstrates using the BaseType property.

using System;
class TestType 
{
    public static void Main() 
    {
        Type t = typeof(int);
        Console.WriteLine("{0} inherits from {1}.", t,t.BaseType);
    }
}

The following example uses recursion to list the complete inheritance hierarchy of each class found in an assembly. The example defines a class named C that derives from a class named B, which, in turn, derives from a class named A.

using System;

public class Example
{
   public static void Main()
   {
      foreach (var t in typeof(Example).Assembly.GetTypes()) {
         Console.WriteLine("{0} derived from: ", t.FullName);
         var derived = t;
         do { 
            derived = derived.BaseType;
            if (derived != null) 
               Console.WriteLine("   {0}", derived.FullName);

         } while (derived != null);
         Console.WriteLine(); 
      } 
   }
}

public class A {} 

public class B : A
{}

public class C : B   
{}
// The example displays the following output: 
//       Example derived from: 
//          System.Object 
//        
//       A derived from: 
//          System.Object 
//        
//       B derived from: 
//          A 
//          System.Object 
//        
//       C derived from: 
//          B 
//          A 
//          System.Object

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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