C# Program to Check a Specified Type is an Interface or not (original) (raw)

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# `

// C# program to check whether the // given type is interface or not using System; using System.Reflection;

// Declare an interface interface myinterface {

// Method in interface
void gfg();

}

class GFG{

// Driver code static void Main() {

// Check the type is interface or not
// Using the IsInterface property
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# `

// C# program to check whether the // given type is interface or not using System; using System.Reflection;

// Declare an interface interface myinterface {

// Method in interface
void gfg();

}

// Declare a class public class myclass { public void myfunc(){} }

// Declare a struct public struct mystruct { int a; }

class GFG{

// Driver code static void Main() {

// Check the type is interface or not
// Using the IsInterface property
Console.WriteLine(typeof(myinterface).IsInterface);
Console.WriteLine(typeof(myclass).IsInterface);
Console.WriteLine(typeof(mystruct).IsInterface);

} }

`

Output:

True False False

Similar Reads