当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。