如何在不使用 C# 中的 sizeof 的情況下查詢變數的大小?
size of 用於獲取變數的大小。
int x; x = sizeof(int);
如需在不使用 sizeof 的情況下獲取變數的大小,請嘗試以下程式碼 −
// without using sizeof byte[] dataBytes = BitConverter.GetBytes(x); int d = dataBytes.Length;
以下是完整程式碼。
示例
using System;
class Demo {
public static void Main() {
int x;
// using sizeof
x = sizeof(int);
Console.WriteLine(x);
// without using sizeof
byte[] dataBytes = BitConverter.GetBytes(x);
int d = dataBytes.Length;
Console.WriteLine(d);
}
}輸出
4 4
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP