C# | Type.IsAssignableFrom(Type) Method - GeeksforGeeks

19 Dec, 2019
Type.IsAssignableFrom(Type) Method is used determine whether an instance of a specified type can be assigned to a variable of the current type.
Syntax: public virtual bool IsAssignableFrom (Type c);Here, it takes the type to compare with the current type.
Return Value: This method returns 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.
Below programs illustrate the use of Type.IsAssignableFrom() Method:
Example 1:
// C# program to demonstrate the// Type.IsAssignableFrom() Methodusing System;using System.Globalization;using System.Reflection;using System.IO;  // Defining MyClass extended// from TypeDelegatorpublic class MyClass : TypeDelegator { }  class GFG {      // Main Method    public static void Main()    {        // Declaring and initializing object of Type        Type objType = typeof(TypeDelegator);          // Getting array of Properties by        // using GetProperties() Method        bool status = objType.IsAssignableFrom(typeof(MyClass));          // Display the Result        if (status)            Console.WriteLine("Instance of a specified type can be "                   + "assigned to a variable of the current type.");        else            Console.WriteLine("Instance of a specified type can't be "                     + "assigned to a variable of the current type.");    }}
Instance of a specified type can be assigned to a variable of the current type.

Example 2:
// C# program to demonstrate the// Type.IsAssignableFrom() Methodusing System;using System.Globalization;using System.Reflection;using System.IO;  class GFG {      // Main Method    public static void Main()    {        // Declaring and initializing object of Type        Type objType = typeof(double);          // Getting array of Properties by        // using GetProperties() Method        bool status = objType.IsAssignableFrom(typeof(int));          // Display the Result        if (status)            Console.WriteLine("Instance of a specified type can be "                   + "assigned to a variable of the current type.");        else            Console.WriteLine("Instance of a specified type can't be "                     + "assigned to a variable of the current type.");    }}
Instance of a specified type can't be assigned to a variable of the current type.

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.type.isassignablefrom?view=netframework-4.8
shubham_singh
CSharp-method
CSharp-Type-Class
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
C# | List Class
C# | Inheritance
Partial Classes in C#
Convert String to Character Array in C#
Lambda Expressions in C#
Difference between Hashtable and Dictionary in C#