如何在不使用 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