Skip to content
Related Articles

Related Articles

Improve Article
C# | Type.GetInterfaces() Method
  • Last Updated : 14 May, 2019

Type.GetInterfaces() Method is used to get all the interfaces implemented or inherited by the current Type when overridden in a derived class.

Syntax: public abstract Type[] GetInterfaces ();

Return Value: This method returns an array of Type objects representing all the interfaces implemented or inherited by the current Type or an empty array of type Type if no interfaces are implemented or inherited by the current Type.

Below programs illustrate the use of Type.GetInterfaces() Method:

Example 1:






// C# program to demonstrate the
// Type.GetInterfaces() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(int);
  
        // Getting interface of specified name
        // using GetField(String) Method
        Type[] minterface = objType.GetInterfaces();
  
        // Display the Result
        Console.WriteLine("Interface present in type {0}", objType);
        for (int i = 0; i < minterface.Length; i++)
            Console.WriteLine(" {0}", minterface[i]);
    }
}
Output:
Interface present in type System.Int32
 System.IFormattable
 System.IComparable
 System.IComparable`1[System.Int32]
 System.IConvertible
 System.IEquatable`1[System.Int32]

Example 2: For if no public field is defined




// C# program to demonstrate the
// Type.GetInterfaces() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(string);
  
        // Getting interface of specified name
        // using GetField(String) Method
        Type[] minterface = objType.GetInterfaces();
  
        // Display the Result
        Console.WriteLine("Interface present in type {0}", objType);
        for (int i = 0; i < minterface.Length; i++)
            Console.WriteLine(" {0}", minterface[i]);
    }
}
Output:
Interface present in type System.String
 System.ICloneable
 System.Collections.Generic.IEnumerable`1[System.Char]
 System.IComparable
 System.IComparable`1[System.String]
 System.IConvertible
 System.Collections.IEnumerable
 System.IEquatable`1[System.String]

Reference:




My Personal Notes arrow_drop_up
Recommended Articles
Page :