當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# Type.GetTypeCode()用法及代碼示例

Type.GetTypeCode()方法用於獲取指定Type的基礎類型代碼。

用法: public static TypeCode GetTypeCode (Type type);
Here, it takes the type whose underlying type code to get.

返回值:此方法返回基礎類型的代碼;如果type為null,則返回Empty。


以下示例程序旨在說明Type.GetTypeCode()方法的使用:

示例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); 
    } 
}
輸出:
TypeCode is Int32

示例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); 
    } 
}
輸出:
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

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.GetTypeCode() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。