c sharp

C# IsAbstract Property

When we are implementing Object Oriented Programming concepts in ot C# Project, we may check the object oriented properties like whether the given class is abstract 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 IsAbstract.
Let’s look into it.

IsAbstract Property

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

Syntax:

typeof(class).IsAbstract

 
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 abstract or not.

using System;
class Linuxhint
{
    static public  void Main(){
       
    //check the Linuxhint is abstract or not
Console.WriteLine("Is Linuxhint Abstract Class or not: "+ typeof(Linuxhint).IsAbstract);
 
}
}

 
Output:


Explanation:

Line 7:

Check if the Class-Linuxhint is abstract or not.

As it is not abstract, False is returned.

Example 2:

Let’s create an abstract class named – Power and check if it is abstract or not.

using System;
class Linuxhint
{
   
    //create Power(abstract class)
    abstract class Power{
        //this is abstract class
    }
    static public  void Main(){
       
    //check the Power is abstract or not
Console.WriteLine("Is Power Abstract Class or not: "+ typeof(Power).IsAbstract);
 
}
}

 
Output:


Explanation:

Line 6:

Here, we created an Abstract class named Power.

Line 12:

Check if the Power is abstract or not.

As it is Abstract, True is returned.

Example 3:

Let’s create an abstract method inside an abstract class.

using System;
class Linuxhint
{
   
    //create Power(abstract class)
    abstract class Power{
        public  void height(){
            Console.WriteLine("Your height is 5.67");

        }
    }
    static public  void Main(){
       
    //check the Power is abstract or not
Console.WriteLine("Is Power is an Abstract class?: "+ typeof(Power).IsAbstract);
 
}
}

 
Output:


Explanation:


Line 7-9:

Here, we created a method named – height inside Power Abstract class.

Line 14:

Check if the Power is abstract or not.

As it is Abstract, True is returned.

Conclusion

In this C# tutorial, we saw how to check if the class is abstract or not using the IsAbstract property. This property tells us by returning a Boolean value with three examples. If it is True, we can say that the class is Abstract class. If it is False, we can say that the class is not an abstract class.

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