C# | Type.GetTypeCode() Method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

TypeCode is Int32

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.