C# | Type.GetInterfaces() Method

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]);
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]);
    }
}

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.