Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# Program to Check a Specified Type is a Value Type or Not

  • Last Updated : 23 Nov, 2021

In C#, the value type represents a sequence of bits. It is not a class or an interface, it is referred to as a struct or enum(a special case of value type). So to check whether the specified type is Value Type or not we use the IsValueType property of the Type class. It is a read-only property. It will return true if the type is Value Type. Otherwise, it will return false. It will return true for enumeration but not return true for Enum type.

Syntax:

public bool IsValueType { get; }

Example 1:

C#




// C# program to check whether the specified
// type is a value type or not
using System;
using System.Reflection;
  
// Declare a structure
struct myStructure
{
      
    // Declaring a method 
    public static void display()
    {
        Console.WriteLine("Hello! GeeksforGeeks");
    }
}
  
// Declare a class
public class Geeks
{
      
    // Declaring a method 
    public static void show()
    {
        Console.WriteLine("Hey! GeeksforGeeks");
    }
      
}
  
public class GFG{
      
// Driver class
public static void Main(string[] args)
{
      
    // Checking the given type is a value type or not
    // Using IsValueType Property
    Console.WriteLine(typeof(myStructure).IsValueType);
    Console.WriteLine(typeof(Geeks).IsValueType);
}
}

Output:

True
False

Example 2:

C#




// C# program to check whether the specified
// type is a value type or not
using System;
using System.Reflection;
  
// Declare a structure
struct myGFG
{
      
    // Declaring a method 
    public static void display()
    {
        Console.WriteLine("Welcome to GeeksforGeeks");
    }
}
  
public class GFG{
      
// Driver class
public static void Main(string[] args)
{
      
    // Checking the given type is a value type or not
    // Using IsValueType Property
    if (typeof(myGFG).IsValueType == true)
    {
        Console.WriteLine("The given type is value type");
    }
    else
    {
        Console.WriteLine("The given type is not a value type");
    }
}
}

Output:

The given type is value type

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!