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


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


此方法用於返回指定對象的TypeCode。

用法:

public static TypeCode GetTypeCode (object value);

在這裏,該值是一個實現IConvertible接口的對象。


返回值:此方法返回值的TypeCode,如果value為null,則返回Empty。

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

示例1:

// C# program to demonstrate 
// Convert.GetTypeCode() Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // Declaring val1 and val2 
        string val1; 
        bool val2; 
  
        // intializing the 
        // val1 and val2 
        val1 = "abab"; 
        val2 = true; 
  
        // getting TypeCode 
        // using Convert.GetTypeCode() method 
        TypeCode t1 = Convert.GetTypeCode(val1); 
        TypeCode t2 = Convert.GetTypeCode(val2); 
  
        // Display TypeCode 
        Console.WriteLine("TypeCode of val1 is {0}", t1); 
        Console.WriteLine("TypeCode of val2 is {0}", t2); 
    } 
}
輸出:
TypeCode of val1 is String
TypeCode of val2 is Boolean

示例2:

// C# program to demonstrate 
// Convert.GetTypeCode() Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // Declaring val1 and val2 
        char val1; 
        float val2; 
  
        // intializing the 
        // val1 and val2 
        val1 = 'a'; 
        val2 = 20f; 
  
        // calling get() method 
        get(val1); 
        get(val2); 
    } 
  
    // Defining the get() method 
    public static void get(char val) 
    { 
  
        // getting TypeCode 
        // using Convert.GetTypeCode() method 
        TypeCode t = Convert.GetTypeCode(val); 
  
        // Display the TypeCode 
        Console.WriteLine("TypeCode of {0}"+ 
                         " is {1}", val, t); 
    } 
    public static void get(float val) 
    { 
  
        // getting TypeCode 
        // using GetTypeCode() method 
        TypeCode t = Convert.GetTypeCode(val); 
  
        // Display the TypeCode 
        Console.WriteLine("TypeCode of {0}"+ 
                         " is {1}", val, t); 
    } 
}
輸出:
TypeCode of a is Char
TypeCode of 20 is Single

參考:



相關用法


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