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 if no interfaces are implemented or inherited by the current Type.
Below programs illustrate the use of Type.GetInterfaces() Method:
Example 1:
C#
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
Type objType = typeof ( int );
Type[] minterface = objType.GetInterfaces();
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#
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
Type objType = typeof ( string );
Type[] minterface = objType.GetInterfaces();
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:
Last Updated :
08 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...