Type::IsAssignableFrom Method (Type^)


Determines whether an instance of a specified type can be assigned to an instance of the current type.

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

virtual bool IsAssignableFrom(
	Type^ c


Type: System::Type^

The type to compare with the current type.

Return Value

Type: System::Boolean

true if any of the following conditions is true:

  • c and the current instance represent the same type.

  • c is derived either directly or indirectly from the current instance. c is derived directly from the current instance if it inherits from the current instance; c is derived indirectly from the current instance if it inherits from a succession of one or more classes that inherit from the current instance.

  • The current instance is an interface that c implements.

  • c is a generic type parameter, and the current instance represents one of the constraints of c.

    In the following example, the current instance is a Type object that represents the Stream class. GenericWithConstraint is a generic type whose generic type parameter must be of type Stream. Passing its generic type parameter to the IsAssignableFrom indicates that an instance of the generic type parameter can be assigned to an Stream object.

    No code example is currently available or this language may not be supported.
  • c represents a value type, and the current instance represents Nullable<c> (Nullable(Of c) in Visual Basic).

false if none of these conditions are true, or if c is null.

The IsAssignableFrom method can be used to determine whether an instance of c can be assigned to an instance of the current type, The method is most useful when you are handling objects whose types are not known at design time and allows for conditional assignment, as the following example shows.

No code example is currently available or this language may not be supported.

This method thus ensures that a line of code like the following will execute at runtime without throwing an InvalidCastException exception or a similar exception:

No code example is currently available or this language may not be supported.

This method can be overridden by a derived class.


A generic type definition is not assignable from a closed constructed type. That is, you cannot assign the closed constructed type MyGenericList<int> (MyGenericList(Of Integer) in Visual Basic) to a variable of type MyGenericList<T>.

If the c parameter is of type TypeBuilder, the result is based on the type that is to be built. The following code example demonstrates this using a built type named B.

No code example is currently available or this language may not be supported.

The following example demonstrates the IsAssignableFrom method using defined classes, integer arrays, and generics.

using namespace System;
using namespace System::Collections::Generic;

ref class Room

ref class Kitchen : Room

ref class Bedroom : Room

ref class Guestroom : Bedroom

ref class MasterBedroom : Bedroom

ref class Program
    static void Main()
            // Demonstrate classes:
            Console::WriteLine("Defined Classes:");
            Room^ room1 = gcnew Room();
            Kitchen^ kitchen1 = gcnew Kitchen();
            Bedroom^ bedroom1 = gcnew Bedroom();
            Guestroom^ guestroom1 = gcnew Guestroom();
            MasterBedroom^ masterbedroom1 = gcnew MasterBedroom();

            Type^ room1Type = room1->GetType();
            Type^ kitchen1Type = kitchen1->GetType();
            Type^ bedroom1Type = bedroom1->GetType();
            Type^ guestroom1Type = guestroom1->GetType();
            Type^ masterbedroom1Type = masterbedroom1->GetType();

            Console::WriteLine("room assignable from kitchen: {0}", room1Type->IsAssignableFrom(kitchen1Type));
            Console::WriteLine("bedroom assignable from guestroom: {0}", bedroom1Type->IsAssignableFrom(guestroom1Type));
            Console::WriteLine("kitchen assignable from masterbedroom: {0}", kitchen1Type->IsAssignableFrom(masterbedroom1Type));

            // Demonstrate arrays:
            Console::WriteLine("Integer arrays:");
            array<Int32>^ array2 = gcnew array<Int32>(2);
            array<Int32>^ array10 = gcnew array<Int32>(10);
            array<Int32, 2>^ array22 = gcnew array<Int32, 2>(2, 2);
            array<Int32, 2>^ array24 = gcnew array<Int32, 2>(2, 4);

            Type^ array2Type = array2->GetType();
            Type^ array10Type = array10->GetType();
            Type^ array22Type = array22->GetType();
            Type^ array24Type = array24->GetType();

            Console::WriteLine("Int32[2] assignable from Int32[10]: {0}", array2Type->IsAssignableFrom(array10Type));
            Console::WriteLine("Int32[2] assignable from Int32[2,4]: {0}", array2Type->IsAssignableFrom(array24Type));
            Console::WriteLine("Int32[2,4] assignable from Int32[2,2]: {0}", array24Type->IsAssignableFrom(array22Type));

            // Demonstrate generics:

            // Note that "int?[]" is the same as "Nullable<int>[]"
            //int?[] arrayNull = new int?[10];
            array<Nullable^>^ arrayNull = gcnew array<Nullable^>(10);
            List<Int32>^ genIntList = gcnew List<Int32>();
            List<Type^>^ genTList = gcnew List<Type^>();

            Type^ arrayNullType = arrayNull->GetType();
            Type^ genIntListType = genIntList->GetType();
            Type^ genTListType = genTList->GetType();

            Console::WriteLine("Int32[10] assignable from Nullable[10]: {0}", array10Type->IsAssignableFrom(arrayNullType));
            Console::WriteLine("List<Int32> assignable from List<Type^>: {0}", genIntListType->IsAssignableFrom(genTListType));
            Console::WriteLine("List<Type^> assignable from List<Int32>: {0}", genTListType->IsAssignableFrom(genIntListType));


int main()

//This code example produces the following output:
// Defned Classes:
// room assignable from kitchen: True
// bedroom assignable from guestroom: True
//kitchen assignable from masterbedroom: False
// Integer arrays:
// Int32[2] assignable from Int32[10]: True
// Int32[2] assignable from Int32[2,4]: False
// Int32[2,4] assignable from Int32[2,2]: True
// Generics:
// Int32[10] assignable from Nullable[10]: False
// List<Int32> assignable from List<Type^>: False
// List<Type^> assignable from List<Int32>: False

.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top