大小()运算符类型运算符、大小、类型

由网友(等风也等你)分享简介:我通常会在我的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 ++ 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);

所以,可能是你正在寻找的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);    

请参阅这里了解更多信息

阅读全文

相关推荐

最新文章