C# | How to get TypeCode for the class String

GetTypeCode() method is used to get the TypeCode of the specified string.
Here TypeCode enum represents a specific type of object. In TypeCode every data type is represented by a specific number like String is represented by 18, Int32 is represented by 9, etc.

Syntax:

public TypeCode GetTypeCode ();

Return value: This method return an enumerated constant.

Below given are some examples to understand the implementation in a better way:

Example 1:




// C# program to illustrate the 
// GetTypeCode() Method
using System;
  
class GFG {
     
    // Main method
    public static void Main()
    {
  
        // variables
        string str1 = "w3wiki";
        int a = 12;
  
        // get the TypeCode by
        // using GetTypeCode() method
        Console.WriteLine("The TypeCode for {0}': {1}",
                              str1, str1.GetTypeCode());
  
        Console.WriteLine("The TypeCode for '{0}': {1}",
                                    a, a.GetTypeCode());
    }
}


Output:

The TypeCode for w3wiki': String
The TypeCode for '12': Int32

Example 2:




// C# program to illustrate the 
// GetTypeCode() Method
using System;
  
class GFG {
   
// Main method
public static void Main()
{
    // given string
    String str1 = "Beginner";
  
    // get the TypeCode of the given String
    // using GetTypeCode() method
    TypeCode g = str1.GetTypeCode();
    Console.WriteLine("TypeCode for '{0}': {1}, Which means it represents a {2}.",
                                          str1, g.ToString("D"), g.ToString("F"));
}
}


Output:

TypeCode for 'Beginner': 18, Which means it represents a String.

Reference:

  • https://docs.microsoft.com/en-us/dotnet/api/system.string.gettypecode?view=netframework-4.7.2