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#
using System;
using System.Reflection;
abstract class Geeks
{
public abstract void geeksmethod();
}
class GFG{
static void Main()
{
if ( typeof (Geeks).IsAbstract == true )
{
Console.WriteLine( "This is abstract" );
}
else
{
Console.WriteLine( "This is not abstract" );
}
}
}
|
Output:
This is abstract
Example 2:
C#
using System;
using System.Reflection;
abstract class Geeks1
{
public abstract void geeksmethod();
}
class Geeks2
{
public void gfgfunc()
{
Console.WriteLine( "This is method" );
}
}
class Geeks3:Geeks1
{
public override void geeksmethod()
{
Console.WriteLine( "This is method" );
}
}
class GFG{
static void Main()
{
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
Share your thoughts in the comments
Please Login to comment...