如何在 C# 中使用陣列類的 GetType 方法?


C# 中的陣列類的 GetType() 方法獲取當前例項的 Type(從 Object 繼承)。

獲取型別。

Type tp = value.GetType();

在以下示例中,我們將使用型別檢查 int 值。

if (tp.Equals(typeof(int)))
Console.WriteLine("{0} is an integer data type.", value)

以下是 GetType() 方法在 C# 中的用法。

示例

using System
public class Program {
   public static void Main() {
      object[] values = { (int) 100, (long) 17111};
      foreach (var value in values) {
         Type tp = value.GetType();
         if (tp.Equals(typeof(int)))
         Console.WriteLine("{0} is aninteger data type.", value);
         else
         Console.WriteLine("'{0}' is not an int data type.", value);
      }
   }
}

更新時間: 23-6-2020

2K+ 次觀看

開啟你的 職業

完成課程以獲取認證

開始
廣告
© . All rights reserved.