c sharp

C# IsClass Property

If you want to check if the Type is Class or not, then you should know about IsClass property. This property is available in Type class.

IsClass Property

The IsClass property from the Type class returns True if the Type is a class. Otherwise, False is returned.

For interface,Enum,Abstract and others, False is returned. For Normal Classes and Delegates with/without modifiers IsClass property Returns True.

Syntax:

bool typeof(Type).IsClass

 
Return Type:

It returns the Boolean value (True/False).

Example 1:

Let’s create an Interface and Enum and check if these are Class Types or not.

using System;
         
         
//create interface
         interface Phone{
             //this is an interface
         }

//create Enum
         enum Orders{
             value1,value2
             //this is an enum
         }      
         
class Linuxhint
{
   
    static public  void Main(){
   

//check the Linuxhint Type is class or not
Console.WriteLine("Is Linuxhint a class?: "+ typeof(Linuxhint).IsClass);

//check the Phone Type is class or not
Console.WriteLine("Is Phone a class?: "+ typeof(Phone).IsClass);

//check the Orders Type is class or not
Console.WriteLine("Is Orders a class?: "+ typeof(Orders).IsClass);

}
}

 
Output:


Explanation:

Line 4-7:

Create Interface.


Line 9-13:

Create an Enum with two values.


Line 21 – 28:

Inside Main method:

Check if interface,Enum and normal class are Type-class or not.


Here, the Enum and interface are not of Type-class, so False is returned. For Linuxhint, it is a class, so True is returned.

Example 2:

In this scenario, we will create two classes with and without a public modifier and check whether they are Type Class or not.

using System;
         
 //create a class with public        
public class Class1{
    //this is class1
}

 //create a class without public      
class Class2{
    //this is class2
}
         
class Linuxhint
{
   
    static public  void Main(){
   

//check the Class1 Type is class or not
Console.WriteLine("Is Class1 a class?: "+ typeof(Class1).IsClass);

//check the Class2 Type is class or not
Console.WriteLine("Is Class2 a class?: "+ typeof(Class2).IsClass);

}
}

 
Output:


Line 3 to Line 11:


Here, both are classes are created.

Line 19 to Line 23:


IsClass property won’t check whether it is public or not. So, True is returned for both of them.

Example 3:

In this scenario, we will create three delegates with and without a public modifier and check whether they are Type Class or not.

using System;
         
 //create a delegate with no parameters      
 delegate void delegate1();  
 
 //create a delegate with 2 parameters      
 delegate void delegate2(int a1,int a2);
 
 //create a delegate with no parameters  and public type    
 public delegate void delegate3();
 
class Linuxhint
{
   
    static public  void Main(){
   

//check the delegate1 Type is class or not
Console.WriteLine("Is delegate1 a class?: "+ typeof(delegate1).IsClass);

//check the delegate2 Type is class or not
Console.WriteLine("Is delegate2 a class?: "+ typeof(delegate2).IsClass);

//check the delegate3 Type is class or not
Console.WriteLine("Is delegate3 a class?: "+ typeof(delegate3).IsClass);
}
}

 
Output:


Line 3 to Line 10:


Here, three delegates are created— delegate1,delegate2 and delegate3.

First delegate has no parameter and returns empty.

Second delegate accepts two parameters and return empty.

Third delegate has no parameters with a public modifier and returns empty.

Line 18 to Line 25:


Check if there are Class Types or not.

As we studied delegates are also Class types. Hence, True is returned.

Closing

In this C# tutorial, we saw how to check if the Type is Class or not using the IsClass property. It returns a Boolean value. If it is True, we can say that the Type is Class and If it is False, we can say that the type is not Class. Notice that the Delegates type is a class and IsClass property won’t check for modifiers.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain