Get all the interfaces implemented or inherited by the current Type in C#

The Type class in C# provides methods to retrieve all interfaces implemented or inherited by a specific type. The GetInterfaces() method returns an array of all interfaces, while GetInterface() method retrieves a specific interface by name.

Syntax

Following is the syntax for getting all interfaces implemented by a type −

Type[] interfaces = type.GetInterfaces();

Following is the syntax for getting a specific interface by name −

Type specificInterface = type.GetInterface("InterfaceName", ignoreCase);

Parameters

  • GetInterface(string name) − Returns the interface with the specified name.

  • GetInterface(string name, bool ignoreCase) − Returns the interface with the specified name, with optional case-insensitive search.

  • GetInterfaces() − Returns an array of all interfaces implemented or inherited by the current type.

Using GetInterfaces() with Built-in Types

Example with Float Type

using System;

public class Demo {
    public static void Main() {
        Type type = typeof(float);
        Type myInterface = type.GetInterface("IFormattable", true);
        Type[] myInterfaces = type.GetInterfaces();
        
        Console.WriteLine("Interface = " + myInterface);
        Console.WriteLine("All the Interfaces...");
        
        for (int i = 0; i < myInterfaces.Length; i++)
            Console.WriteLine("" + myInterfaces[i]);
    }
}

The output of the above code is −

Interface = System.IFormattable
All the Interfaces...
System.IComparable
System.IFormattable
System.IConvertible
System.IComparable`1[System.Single]
System.IEquatable`1[System.Single]

Example with Integer Type

using System;

public class Demo {
    public static void Main() {
        Type type = typeof(int);
        Type myInterface = type.GetInterface("IFormattable");
        Type[] myInterfaces = type.GetInterfaces();
        
        Console.WriteLine("Interface = " + myInterface);
        Console.WriteLine("All the Interfaces...");
        
        for (int i = 0; i < myInterfaces.Length; i++)
            Console.WriteLine("" + myInterfaces[i]);
    }
}

The output of the above code is −

Interface = System.IFormattable
All the Interfaces...
System.IComparable
System.IFormattable
System.IConvertible
System.IComparable`1[System.Int32]
System.IEquatable`1[System.Int32]

Using GetInterfaces() with Custom Classes

Example

using System;
using System.Collections;

public class MyClass : IComparable, IEnumerable {
    public int CompareTo(object obj) {
        return 0;
    }
    
    public IEnumerator GetEnumerator() {
        return null;
    }
}

public class Demo {
    public static void Main() {
        Type type = typeof(MyClass);
        Type[] interfaces = type.GetInterfaces();
        
        Console.WriteLine("Interfaces implemented by MyClass:");
        foreach (Type interfaceType in interfaces) {
            Console.WriteLine(interfaceType.Name);
        }
        
        Console.WriteLine("\nChecking for specific interface:");
        Type comparableInterface = type.GetInterface("IComparable");
        Console.WriteLine("IComparable found: " + (comparableInterface != null));
    }
}

The output of the above code is −

Interfaces implemented by MyClass:
IComparable
IEnumerable
Checking for specific interface:
IComparable found: True

Common Interface Types

Interface Description
IComparable Defines a method for comparing objects
IFormattable Provides functionality to format values into string representations
IConvertible Defines methods for converting values to other types
IEquatable<T> Defines a method for determining equality of instances

Conclusion

The GetInterfaces() method returns all interfaces implemented by a type, while GetInterface() retrieves a specific interface by name. These methods are essential for reflection scenarios where you need to examine the interface contracts that a type supports at runtime.

Updated on: 2026-03-17T07:04:36+05:30

611 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements