Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# Program to Check a Specified Type is a Class or Not

  • Last Updated : 27 Dec, 2021

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 class. Otherwise, it will return false(for structure or enumerators). It is a read-only property.

Syntax:

public bool IsClass { get; }

Example 1:

C#




// C# program to check the given
// type is a class or not
using System;
using System.Reflection;
 
// Declare a class
public class Student1
{
    public void myfun()
    {
        Console.WriteLine("I like DSA");
    }
}
 
// Declare delegates
public delegate void divnum(int x, int y);
 
// Declare structure
public struct Student2
{
    public int Id;
    public string Name;
}
 
public class GFG{
     
// Driver code
public static void Main(string[] args)
{
     
    // Check the given type is a class or not
    // Using IsClass property
    Console.WriteLine(typeof(Student1).IsClass);
    Console.WriteLine(typeof(divnum).IsClass);
    Console.WriteLine(typeof(Student2).IsClass);
}
}

Output:

True
True
False

Example 2:

C#




// C# program to check the given
// type is a class or not
using System;
using System.Reflection;
 
// Declare a class
public class Pet
{
    public void myfun()
    {
        Console.WriteLine("I like Dogs");
    }
}
 
public class GFG{
     
// Driver code
public static void Main(string[] args)
{
     
    // Check the given type is a class or not
    // Using IsClass property
    if (typeof(Pet).IsClass == true)
    {
        Console.WriteLine("The given type is a class");
    }
    else
    {
        Console.WriteLine("The given type is not a class");
    }
}
}

Output:

The given type is a class

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!