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 class. Otherwise, it will return false(for structure or enumerators). It is a read-only property.
Syntax:
public bool IsClass { get; }
Example 1:
C#
using System;
using System.Reflection;
public class Student1
{
public void myfun()
{
Console.WriteLine( "I like DSA" );
}
}
public delegate void divnum( int x, int y);
public struct Student2
{
public int Id;
public string Name;
}
public class GFG{
public static void Main( string [] args)
{
Console.WriteLine( typeof (Student1).IsClass);
Console.WriteLine( typeof (divnum).IsClass);
Console.WriteLine( typeof (Student2).IsClass);
}
}
|
Output:
True
True
False
Example 2:
C#
using System;
using System.Reflection;
public class Pet
{
public void myfun()
{
Console.WriteLine( "I like Dogs" );
}
}
public class GFG{
public static void Main( string [] args)
{
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
Last Updated :
03 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...