Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# Program to Check a Specified Type is a Primitive Data Type or Not

  • Last Updated : 16 Nov, 2021

In C#, data types are used to specify the type of data that a variable can hold. There are two types of data types available in C# that is, primitive and non-primitive data types. Primitive data types are predefined data types such as Byte, SByte, Boolean, Int16, UInt16, Int32, UInt32, Char, Double, Int64, UInt64, Single, etc. Whereas non-primitive data types are user-defined data types such as enum, class, etc. In this article, we will learn how to check a specified type is a primitive data type or not. So, to do this task we use the IsPrimitive property of the Type class. This property is used to check whether the type of the specified data is one of the primitive types or not. It returns true if the given data type is primitive otherwise it will return false.

Syntax:

public bool IsPrimitive{ get; }

Example:

C#




// C# program to check a specified type 
// is a primitive data type or not
using System;
using System.Reflection;
  
class GFG{
  
static void Main()
{
      
    // Check the int is an primitiva or not
    if (typeof(int).IsPrimitive == true)
    {
        Console.WriteLine("Primitive data type");
    }
    else 
    {
        Console.WriteLine("Not a primitive data type");
    }
      
    // Check the float is an primitiva or not
    if (typeof(float).IsPrimitive == true
    {
        Console.WriteLine("Primitive data type");
    }
    else 
    {
        Console.WriteLine("Not a primitive data type");
    }
      
    // Check the int is an primitiva or not
    if (typeof(double).IsPrimitive == true
    {
        Console.WriteLine("Primitive data type");
    }
    else 
    {
        Console.WriteLine("Not a primitive data type");
    }
}
}

Output:

Primitive data type
Primitive data type
Primitive data type


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!