如何使用 sizeof() 運算子查詢 C# 中的資料型別或變數的大小


sizeof() 資料型別返回資料型別的大小。假設你需要查詢 int 資料型別的大小 −

sizeof(int);

對於雙精度資料型別

sizeof(double);

我們來看一個完整的示例來查詢各種資料型別的大小 −

示例

 線上演示

using System;

namespace Demo {

   class Program {

      static void Main(string[] args) {

         Console.WriteLine("The size of int is {0}", sizeof(int));
         Console.WriteLine("The size of int is {0}", sizeof(char));
         Console.WriteLine("The size of short is {0}", sizeof(short));
         Console.WriteLine("The size of long is {0}", sizeof(long));
         Console.WriteLine("The size of double is {0}", sizeof(double));

         Console.ReadLine();
      }
   }
}

輸出

The size of int is 4
The size of int is 2
The size of short is 2
The size of long is 8
The size of double is 8

更新於:2020-06-20

261 次瀏覽

開啟你的 事業

透過完成課程獲得認證

開始
廣告