C# Program to Check a Specified Class is an Abstract Class or Not (original) (raw)
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
Similar Reads
- C# Program to Check a Specified Class is a Sealed Class or not A Sealed class is a class that will not let the users inherit the class. We can create a sealed class using sealed keywords. This keyword tells the compiler that the class is a sealed class. In this article, we will learn how to check the specified class is a sealed class or not. So we use the IsSea 2 min read
- C# Program to Check a Class is a Sub-Class of a Specified Class or Not A class is a collection of methods, variables, and objects. A subclass is a class that is extended from the parent class. It should achieve all the properties from the parent class. Its syntax is similar to a class. Using: operator we can create the subclass. We can check the class is a subclass of 2 min read
- C# Program to Check a Specified Type is a Class or Not A class is a collection of methods, variables, and objects. Or we can say that a class is a blueprint using which an object is created. So to check whether the specified type is a class as well as delegates or not we use the IsClass property of the Type class. It will return true if the type is clas 2 min read
- C# Program to Check a Specified class is a Serializable class or not Serialization is a method of converting an object into a stream of bytes that will be used to store the object in the database, or memory, or file, etc so that we can easily read back and convert it back to an object. So to check whether a specified class is serializable or not we use the IsSerializ 2 min read
- C# Program to Check a Specified Type is an Interface or not The interface is just like a class, it can also have methods, properties, events, etc. as its members, but it only contains the declaration of the members and the implementation of these members will be given by the class that implements the interface implicitly or explicitly. We can check the speci 2 min read
- C# Program to Check a Specified Type is an Array or Not In C#, an array is a group of homogeneous elements that are referred to by a common name. So in this article, we will discuss how to check the variable is of array type or not. To do this task, we use the IsArray property of the Type class. This property is used to determine whether the specified ty 2 min read
- C# Program to Check a Specified Type is an Enum or Not Enum or also known as Enumeration is used to store user define data. It is used to assign the string value to an integral constant which makes programs easy to read and manage. We can create enum data using the enum keyword followed by the enum name. In C#, we can check the specific type is enum or 2 min read
- C# Program to Check a Specified Type is Nested or not In programming languages, nested means a class or a method or loop or structure is present inside another class or a method or loop or structure. In C#, we can check a specified type is nested or not using the IsNested property of the Type class. This property returns a value that represents whether 2 min read
- C# Program to Check a Specified Type is a Pointer or not A pointer is a variable that contains the references of another variable. Or in other words, the pointer is a variable that stores the address of the same type of variable. For example, a string pointer can store the address of a string. In C#, we can able to check the given type is a pointer or not 2 min read
- C# Program to Check a Specified Type is a Primitive Data Type or Not In C#, data types are used to specify the type of data that a variable can hold. There are two types of data types available in C# that is, primitive and non-primitive data types. Primitive data types are predefined data types such as Byte, SByte, Boolean, Int16, UInt16, Int32, UInt32, Char, Double, 2 min read