您的位置:首页 > 编程语言 > C#

C#中sizeof使用

2016-05-30 11:35 435 查看
转载自:http://www.cnblogs.com/stublue/archive/2010/02/01/1661219.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
class Program
{
static void Main(string[] args)
{
int val = 8;
//Console.WriteLine(sizeof(val)); //这个会报错。
Console.WriteLine(sizeof(int)); //这个正常,跟c里的一样

Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(val)); //这个能正常输出,查文档得到。
//Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(int));//这个会报错。
Console.ReadKey();
}
}
}


从上面这个程序来看,
以类型定义关键字作为参数得到 类型大小时,
这样使用 sizeof(type); //type 指int double 等类型
以变量作为参数,得到变量所占空间大小时,
这样使用 System.Runtime.InteropServices.Marshal.sizeof(val); // val指一个变量名
由于时间关系,就写了一个int类型的测试,其他类型的以后用到的时候再测试一下吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: