The source code to check a specified type is a class or not is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to check a specified type
//is a class or not.
using System;
using System.Reflection;
class Sample
{
public void Print()
{
Console.WriteLine("Print() method called");
}
}
class Program
{
static void Main()
{
Type type = typeof(Sample);
if (type.IsClass == true)
{
Console.WriteLine("Sample is a class");
}
else
{
Console.WriteLine("Sample is not a class");
}
}
}
Output:
Sample is a class
Press any key to continue . . .
Explanation:
In the above program, we created two classes Sample and Program. The Sample class contains an instance method Print(). The Program class is a non-abstract class that contains the Main() method. The Main() method is the entry point for the program. Here, we check the specified type is a class or not using the IsClass property of Type class and printed the appropriate message on the console screen.
Program:
The source code to check a specified type is a class or not is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
In the above program, we created two classes Sample and Program. The Sample class contains an instance method Print(). The Program class is a non-abstract class that contains the Main() method. The Main() method is the entry point for the program. Here, we check the specified type is a class or not using the IsClass property of Type class and printed the appropriate message on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer