C# 的所有資料型別都有哪些基類?


Object 是 C# 中所有資料型別的基類。Object 型別是 C# 公共型別系統 (CTS) 中所有資料型別的最終基類。object 是 System.Object 類的別名。

當值型別轉換為物件型別時,稱為裝箱;另一方面,當物件型別轉換為值型別時,稱為拆箱。

以下示例演示瞭如何使用物件資料型別 -

using System;
using System.IO;

namespace Demo {
   class objectClass {
      public int x = 56;
   }

   class MyApplication {
      static void Main() {
         object obj;
         obj = 96;
         Console.WriteLine(obj);
         obj = new objectClass();
         objectClass newRef;
         newRef = (objectClass)obj;
         Console.WriteLine(newRef.x);
      }
   }  
}

更新於: 20-Jun-2020

1K+ 瀏覽量

開啟你的 職業

完成課程並獲得認證

開始
廣告
© . All rights reserved.