c sharp

C# IsPrimitive Property

Sometimes, we create and include variables in our C# application. So, we can create variables by specifying the data types. Data types are classified into multiple categories. In this tutorial, let’s see how to check if the specified Type is a primitive type or not.

IsPrimitive Property

IsPrimitive property is used to check if the given data type is primitive. If the Type is primitive, it returns True; otherwise, False.

Syntax:

typeof(Type).IsPrimitive

Return Type:

It returns the Boolean value (True/False).

Suppose if you want to return the Type, use the Name property.

Syntax:

Type.Name

List of Primitive Types:

  1. Boolean – It is used to store Boolean values
  2.  Byte – It stores byte values
  3. SByte – It stores Signed byte values
  4. Int16 – It stores Integer values in 16 bits
  5. UInt16 – It stores Unsigned  Integer values in 16 bits
  6. Int32 –  It stores Integer values in 32 bits
  7. UInt32 – It stores Unsigned Integer values in 16 bits
  8. Int64 – It stores Integer values in 64 bits
  9. UInt64 – It stores Unsigned Integer values in 64 bits
  10. IntPtr – It stores Integer pointer values
  11. UIntPtr – It stores Unsigned Integer values in 16 bits
  12. Char – It stores Characters
  13. Double – It stores Double type
  14. Single – It stores values in a single stricture/float type

Example 1:

Let’s check if the types are Primitive types or not.

using System;
class Linuxhint
{
    static public  void Main(){
   
    //check the Type - SByte
    Console.WriteLine("Type: "+typeof(SByte).Name + " and Is it Primitive ?: "+typeof(SByte).IsPrimitive);
   
     //check the Type - Boolean
    Console.WriteLine("Type: "+typeof(Boolean).Name + " and Is it Primitive ?: "+typeof(Boolean).IsPrimitive);
   
    //check the Type - Byte
    Console.WriteLine("Type: "+typeof(Byte).Name + " and Is it Primitive ?: "+typeof(Byte).IsPrimitive);
   
    //check the Type - Int16
    Console.WriteLine("Type: "+typeof(Int16).Name + " and Is it Primitive ?: "+typeof(Int16).IsPrimitive);
   
    //check the Type - UInt16
    Console.WriteLine("Type: "+typeof(UInt16).Name + " and Is it Primitive ?: "+typeof(UInt16).IsPrimitive);
   
    //check the Type - Int32
    Console.WriteLine("Type: "+typeof(Int32).Name + " and Is it Primitive ?: "+typeof(Int32).IsPrimitive);
   
    //check the Type - UInt32
    Console.WriteLine("Type: "+typeof(UInt32).Name + " and Is it Primitive ?: "+typeof(UInt32).IsPrimitive);
   
    //check the Type - Int64
    Console.WriteLine("Type: "+typeof(Int64).Name + " and Is it Primitive ?: "+typeof(Int64).IsPrimitive);
   
    //check the Type - UInt64
    Console.WriteLine("Type: "+typeof(UInt64).Name + " and Is it Primitive ?: "+typeof(UInt64).IsPrimitive);
   
    //check the Type - IntPtr
    Console.WriteLine("Type: "+typeof(IntPtr).Name + " and Is it Primitive ?: "+typeof(IntPtr).IsPrimitive);
   
    //check the Type - UIntPtr
    Console.WriteLine("Type: "+typeof(UIntPtr).Name + " and Is it Primitive ?: "+typeof(UIntPtr).IsPrimitive);
   
    //check the Type - Char
    Console.WriteLine("Type: "+typeof(Char).Name + " and Is it Primitive ?: "+typeof(Char).IsPrimitive);
   
    //check the Type - Double
    Console.WriteLine("Type: "+typeof(Double).Name + " and Is it Primitive ?: "+typeof(Double).IsPrimitive);
   
    //check the Type - Single
    Console.WriteLine("Type: "+typeof(Single).Name + " and Is it Primitive ?: "+typeof(Single).IsPrimitive);
   
}
}

Output:

Explanation:

Inside Main method –

Line 7 – Line 46: we are checking if the 14 Types are primitive or not. All are under Primitive types. So, the output is True for all 14 types.

Example 2:

Check Array and Enum Types are primitive or not.

using System;
class Linuxhint
{
    static public  void Main(){
   
    //check the Type - array
    Console.WriteLine("Type: "+typeof(Array).Name + " and Is it Primitive ?: "+typeof(Array).IsPrimitive);
   
    //check the Type - enum
    Console.WriteLine("Type: "+typeof(Enum).Name + " and Is it Primitive ?: "+typeof(Enum).IsPrimitive);
 
   
}
}

Output:

They are not primitive since the output is False.

Summary

In this C# tutorial, we saw how to check if the Type is primitive or not using IsPrimitive property. This property tells us by returning a Boolean value. If it is True, we can say that the Type is Primitive and If it is False, we can say that the Type is Not Primitive. There are totally 14 Primitive 14 Types in C#.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain