The source code to check a specified type is a value type 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 value type or not.
using System;
using System.Reflection;
struct Sample
{
public static void Print()
{
Console.WriteLine("Print() method called");
}
}
class Program
{
static void Main()
{
Type type = typeof(Sample);
if (type.IsValueType == true)
{
Console.WriteLine("Sample is value type");
}
else
{
Console.WriteLine("Sample is not value type");
}
}
}
Output:
Sample is value type
Press any key to continue . . .
Explanation:
In the above program, we created a structure and a class Program. The Sample structure contains a static Method Print(), and the Program class contains the Main() method. The Main() method is the entry point for the program. Here we check the specified type is a value type or not using the IsValueType property of Type class and printed the appropriate message on the console screen.
Program:
The source code to check a specified type is a value type 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 a structure and a class Program. The Sample structure contains a static Method Print(), and the Program class contains the Main() method. The Main() method is the entry point for the program. Here we check the specified type is a value type or not using the IsValueType property of Type class and printed the appropriate message on the console screen.