大小()运算符类型 [英] sizeof() operator for types

查看:134
本文介绍了大小()运算符类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常会在我的C ++ code做到这一点:

I would normally do this in my C++ code:

int variable = 10;
int sizeOfVariable = sizeof(variable);   //Returns 4 for 32-bit process

但是,这似乎并不适用于C#。是否有一个模拟?

But that doesn't seem to work for C#. Is there an analog?

推荐答案

的sizeof 运营商在C#中只能在编译时已知类型,而不是变量(实例)。

The sizeof operator in C# works only on compile-time known types, not on variables (instances).

正确的例子是

int variable = 10;
int sizeOfVariable = sizeof(int);

所以,可能是你正在寻找的<一个href="http://msdn.microsoft.com/library/system.runtime.interopservices.marshal.sizeof"><$c$c>Marshal.SizeOf它可以用于任何对象实例或运行时类型

So probably you are looking for Marshal.SizeOf which can be used on any object instances or runtime types.

int variable = 10;
int sizeOfVariable = Marshal.SizeOf(variable);    

请参阅这里了解更多信息

这篇关于大小()运算符类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆