Home »
.Net »
C# Programs
C# - Check a Specified Type is Primitive Data Type or Not?
Learn, how to check a specified type is a primitive data type or not in C#?
Submitted by Nidhi, on October 30, 2020 [Last updated : March 22, 2023]
Checking a type is primitive data type or not
Here, we will check a specified type is primitive data-type or not using the IsPrimitive property of Type class.
C# program to check a specified type is primitive data type or not
The source code to check a specified type is a primitive data-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 primitive data type.
using System;
using System.Reflection;
class Program
{
static void Main()
{
Type type = typeof(int);
if (type.IsPrimitive== true)
{
Console.WriteLine("\"int\" is a primitive data type");
}
else
{
Console.WriteLine("\"int\" is not a primitive data type");
}
}
}
Output
"int" is a primitive data type
Press any key to continue . . .
Explanation
In the above program, we created a class Program that contains the Main() method. The Main() method is the entry point for the program. Here we check the specified type is primitive data-type or not using the IsPrimitive property of Type class and printed the appropriate message on the console screen.
C# Basic Programs »