Type.GetInterface() Method in C#

The Type.GetInterface() method in C# is used to get a specific interface implemented or inherited by the current Type. This method searches through all interfaces implemented by a type and returns the one that matches the specified name.

Syntax

Following are the overloads of the GetInterface() method −

public Type GetInterface(string name);
public abstract Type GetInterface(string name, bool ignoreCase);

Parameters

  • name − The name of the interface to find.

  • ignoreCase − A Boolean value indicating whether to perform a case-insensitive search for the interface name.

Return Value

Returns a Type object representing the interface with the specified name, or null if no interface with that name is found.

Using GetInterface() with Built-in Types

Example

using System;

public class Demo {
    public static void Main() {
        Type type = typeof(double);
        Type myInterface = type.GetInterface("IFormattable");
        Console.WriteLine("Interface = " + myInterface);
        
        // Check if interface exists
        if (myInterface != null) {
            Console.WriteLine("Double implements IFormattable interface");
        }
    }
}

The output of the above code is −

Interface = System.IFormattable
Double implements IFormattable interface

Using GetInterface() with Case-Insensitive Search

Example

using System;

public class Demo {
    public static void Main() {
        Type type = typeof(float);
        
        // Case-sensitive search (will return null)
        Type myInterface1 = type.GetInterface("iformattable");
        Console.WriteLine("Case-sensitive search: " + myInterface1);
        
        // Case-insensitive search (will find the interface)
        Type myInterface2 = type.GetInterface("iformattable", true);
        Console.WriteLine("Case-insensitive search: " + myInterface2);
    }
}

The output of the above code is −

Case-sensitive search: 
Case-insensitive search: System.IFormattable

Using GetInterface() with Custom Classes

Example

using System;

interface IDrawable {
    void Draw();
}

interface IMoveable {
    void Move();
}

public class Shape : IDrawable, IMoveable {
    public void Draw() {
        Console.WriteLine("Drawing shape");
    }
    
    public void Move() {
        Console.WriteLine("Moving shape");
    }
}

public class Demo {
    public static void Main() {
        Type shapeType = typeof(Shape);
        
        Type drawableInterface = shapeType.GetInterface("IDrawable");
        Type moveableInterface = shapeType.GetInterface("IMoveable");
        Type nonExistentInterface = shapeType.GetInterface("IPrintable");
        
        Console.WriteLine("IDrawable interface: " + drawableInterface?.Name);
        Console.WriteLine("IMoveable interface: " + moveableInterface?.Name);
        Console.WriteLine("IPrintable interface: " + nonExistentInterface);
    }
}

The output of the above code is −

IDrawable interface: IDrawable
IMoveable interface: IMoveable
IPrintable interface: 

Conclusion

The Type.GetInterface() method provides a way to dynamically discover and retrieve specific interfaces implemented by a type at runtime. It supports both case-sensitive and case-insensitive searches, making it useful for reflection scenarios where you need to check interface implementation programmatically.

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

275 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements