C# Program to Check a Specified Type is a Value Type or Not
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#
using System;
using System.Reflection;
struct myStructure
{
public static void display()
{
Console.WriteLine( "Hello! GeeksforGeeks" );
}
}
public class Geeks
{
public static void show()
{
Console.WriteLine( "Hey! GeeksforGeeks" );
}
}
public class GFG{
public static void Main( string [] args)
{
Console.WriteLine( typeof (myStructure).IsValueType);
Console.WriteLine( typeof (Geeks).IsValueType);
}
}
|
Output:
True
False
Example 2:
C#
using System;
using System.Reflection;
struct myGFG
{
public static void display()
{
Console.WriteLine( "Welcome to GeeksforGeeks" );
}
}
public class GFG{
public static void Main( string [] args)
{
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
Last Updated :
07 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...