Type.GetTypeCode() Method is used to get the underlying type code of the specified Type.
Syntax: public static TypeCode GetTypeCode (Type type);
Here, it takes the type whose underlying type code to get.Return Value: This method returns the code of the underlying type, or Empty if type is null.
Below programs illustrate the use of Type.GetTypeCode() Method:
Example 1:
// C# program to demonstrate the // Type.GetTypeCode() Method using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// creating and initializing object Type
Type type = typeof ( int );
// Getting the TypeCode
TypeCode code = Type.GetTypeCode(type);
// Display the Result
Console.WriteLine( "TypeCode is {0}" , code);
}
} |
Output:
TypeCode is Int32
Example 2:
// C# program to demonstrate the // Type.GetTypeCode() Method using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Calling get() Method
get ( typeof ( int ));
get ( typeof ( decimal ));
get ( typeof ( double ));
get ( typeof ( short ));
get ( typeof ( string ));
}
// defining get Method
public static void get (Type type)
{
// Getting Typecode
TypeCode code = Type.GetTypeCode(type);
// Display the Result
Console.WriteLine( "TypeCode of {1} is {0}" , code, type);
}
} |
Output:
TypeCode of System.Int32 is Int32 TypeCode of System.Decimal is Decimal TypeCode of System.Double is Double TypeCode of System.Int16 is Int16 TypeCode of System.String is String
Reference: