📜  C#|如何获取String类的TypeCode

📅  最后修改于: 2021-05-29 18:35:49             🧑  作者: Mango

GetTypeCode()方法用于获取指定字符串的TypeCode
这里的TypeCode枚举表示对象的特定类型。在TypeCode中,每个数据类型都由一个特定的数字表示,例如String表示为18,Int32表示为9,等等。

句法:

public TypeCode GetTypeCode ();

返回值:该方法返回一个枚举常量。

下面给出了一些示例,以更好地理解实现:

范例1:

// C# program to illustrate the 
// GetTypeCode() Method
using System;
  
class GFG {
     
    // Main method
    public static void Main()
    {
  
        // variables
        string str1 = "Geeksforgeeks";
        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());
    }
}

输出:

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

范例2:

// C# program to illustrate the 
// GetTypeCode() Method
using System;
  
class GFG {
   
// Main method
public static void Main()
{
    // given string
    String str1 = "Geeks";
  
    // 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"));
}
}

输出:

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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system。字符串.gettypecode?view = netframework-4.7.2