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:
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.
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.
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.
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.