Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# Program to Check a Specified Class is an Abstract Class or Not

  • Last Updated : 16 Nov, 2021

Abstraction is the process to hide the internal details and showing only the functionality. The abstract keyword is used before the class or method to declare the class or method as abstract. In this article, we will learn how to check a specified class is an abstract class or not. To do this task we use the IsAbstract property of the Type class. This property is used to check whether the given Type(i.e., class name) is abstract and must be overridden or not. It will return true if the specified Type(i.e., class name) is abstract. Otherwise, return false.

Syntax:

public bool IsAbstract { get; }

Example 1:

C#




// C# program to check a specified class is
// an abstract class or not
using System;
using System.Reflection;
  
// Declare an abstract class named Geeks
abstract class Geeks 
{
      
    // Abstract method
    public abstract void geeksmethod();
}
  
class GFG{
      
static void Main()
{
      
    // Get the type of class by using typeof() function
    // Check the class is abstract or not by using
    // IsAbstract property
    if (typeof(Geeks).IsAbstract == true
    {
        Console.WriteLine("This is abstract");
    }
    else 
    {
        Console.WriteLine("This is not abstract");
    }
}
}

Output:

This is abstract

Example 2:

C#




// C# program to check a specified class is
// an abstract class or not
using System;
using System.Reflection;
  
// Declare an abstract class named Geeks
abstract class Geeks1 
{
      
    // Abstract method
    public abstract void geeksmethod();
}
  
// Declare a class named Geeks2
class Geeks2
{
      
    // Method 
    public void gfgfunc()
    {
        Console.WriteLine("This is method");
    }
}
  
// Declare a class named Geeks3
// It implement abstract class
class Geeks3:Geeks1 
      
    // Method 
    public override void geeksmethod()
    {
        Console.WriteLine("This is method");
    }
}
  
class GFG{
  
// Driver code
static void Main()
{
  
    // Get the type of class by using typeof() function
    // Check the class is abstract or not by using
    // IsAbstract property
    bool res1 = typeof(Geeks1).IsAbstract;
    bool res2 = typeof(Geeks2).IsAbstract;
    bool res3 = typeof(Geeks3).IsAbstract;
      
    Console.WriteLine("Is Geeks1 class is abstract class?" + res1);
    Console.WriteLine("Is Geeks2 class is abstract class?" + res2);
    Console.WriteLine("Is Geeks3 class is abstract class?" + res3);
}
}

Output:

Is Geeks1 class is abstract class?True
Is Geeks2 class is abstract class?False
Is Geeks3 class is abstract class?False


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!