如何在C#中不使用sizeof的情况下找到变量的大小?
要获取变量的大小,请使用sizeof。
int x; x = sizeof(int);
要获取变量的大小而不使用sizeof,请尝试以下代码-
// without使用sizeof- byte[] dataBytes = BitConverter.GetBytes(x); int d = dataBytes.Length;
这是完整的代码。
示例
using System;
class Demo {
public static void Main() {
int x;
//使用sizeof-
x = sizeof(int);
Console.WriteLine(x);
// without使用sizeof-
byte[] dataBytes = BitConverter.GetBytes(x);
int d = dataBytes.Length;
Console.WriteLine(d);
}
}输出结果
4 4
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短