C# Program to Check a Specified Type is an Interface or not
Last Updated :
23 Nov, 2021
The interface is just like a class, it can also have methods, properties, events, etc. as its members, but it only contains the declaration of the members and the implementation of these members will be given by the class that implements the interface implicitly or explicitly. We can check the specified type is an interface or not by using the IsInterface property of the Type class. It will return true if the given type is an interface. Otherwise, it will return false. It is a read-only property.
Syntax:
public bool IsInterface { get; }
Example 1:
C#
using System;
using System.Reflection;
interface myinterface
{
void gfg();
}
class GFG{
static void Main()
{
if ( typeof (myinterface).IsInterface == true )
{
Console.WriteLine( "Yes it is Interface" );
}
else
{
Console.WriteLine( "No it is not an Interface" );
}
}
}
|
Output:
Yes it is Interface
Example 2:
C#
using System;
using System.Reflection;
interface myinterface
{
void gfg();
}
public class myclass
{
public void myfunc(){}
}
public struct mystruct
{
int a;
}
class GFG{
static void Main()
{
Console.WriteLine( typeof (myinterface).IsInterface);
Console.WriteLine( typeof (myclass).IsInterface);
Console.WriteLine( typeof (mystruct).IsInterface);
}
}
|
Output:
True
False
False
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...