c sharp

C# IsInterface Property

When we are implementing Object Oriented Programming concepts in ot C# Project, we may check the object-oriented properties like whether the given Type is an interface or not. In this scenario, we have to open the application and check the code statements. In C#, without doing this hectic task, Type class in C# provides a property known as IsInterface.
Let’s look into it.

Interface is used to achieve 100% Abstraction. Thus, we can hide internal details by specifying the functionality.

Structure:


interface interface_name
{
//this is an interface
//we can declare methods
//we can declare properties
//we can declare events
//we can declare indexers
}

IsInterface Property

The IsInterface property from the Type class checks if the specified Type is an interface or not. If it is an interface, True is returned. Otherwise, False is returned.

Syntax:

typeof(Type).IsInterface

Return Type:

It returns the Boolean value (True/False).

Example 1:

Let’s create C# Application with class named – Linuxhint and check if it is an interface or not.

using System;
class Linuxhint
{
    static public  void Main(){
       
    //check the Linuxhint is interface  or not
Console.WriteLine("Is Linuxhint Interface or not: "+ typeof(Linuxhint).IsInterface);
 
}
}

Output:

Explanation:

Line 7:

Check if the Class-Linuxhint is interface or not
As it is not an interface, False is returned.

Example 2:

Let’s create an interface named – switch_button and check if it is an interface or not.

using System;
class Linuxhint
{
   
    //create switch_button(interface)
    interface switch_button{
        //this is an interface
    }
   
    static public  void Main(){
       
    //check the switch_button is interface or not
Console.WriteLine("Is switch_button Interface or not: "+ typeof(switch_button).IsInterface);
 
}
}

Output:

Explanation:

Line 6:

Here, we created an Interface named – switch_button

Line 13:

Check if the switch_button is an interface or not.
As it is Interface, True is returned.

Example 3:

Let’s declare some methods inside an interface.

using System;
class Linuxhint
{
   
    //create switch_button(interface)
    interface switch_button{
        //this is an interface
       
        //method1
         void power_on();
         
         //method2
         void power_off();
         
         //method3
         void power_sleep();
    }
   
    static public  void Main(){
       
    //check the switch_button is interface or not
Console.WriteLine("Is switch_button Interface or not: "+ typeof(switch_button).IsInterface);
 
}
}

Output:

Explanation:

Line 6-17:

Here, we created an Interface named swich_button and declared three methods: power_on. power_off, and power_sleep.

Line 22:

Check if the swich_button is an interface or not.
As it is an interface, True is returned.

Conclusion

In this C# tutorial, we saw how to check if theType is an interface or not using the IsInterface property. This property tells us that by returning a Boolean value with three examples. If it is True, we can say that the Type is Interface and If it is False, we can say that the type is not an interface.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain