C# 类型的 sizeof() 运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18039000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:07:11  来源:igfitidea点击:

sizeof() operator for types

c#.net

提问by c00000fd

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

我通常会在我的 C++ 代码中这样做:

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

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

但这似乎不适用于 C#。有模拟吗?

采纳答案by hazzik

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

sizeofC# 中的运算符仅适用于编译时已知类型,而不适用于变量(实例)。

The correct example would be

正确的例子是

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

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

因此,您可能正在寻找Marshal.SizeOf可用于任何对象实例或运行时类型的对象。

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

See herefor more information

请参阅此处了解更多信息

回答by Mitch Wheat

.NET 4.0 onwards:

.NET 4.0 以后:

if (Environment.Is64BitProcess)
    Console.WriteLine("64-bit process");
else
    Console.WriteLine("32-bit process");

Older versions of .NET framework:

.NET 框架的旧版本:

public static bool Is64BitProcess
{
    get { return IntPtr.Size == 8; }
}

(From your example I'm assuming you want to do this to determine the bitness of the process, which may in fact not be what you are trying to do!)

从你的例子中,我假设你想这样做来确定过程的位数,这实际上可能不是你想要做的!)

回答by dasblinkenlight

You can use the Marshal.SizeOf()method, or use sizeofin unmanaged code:

您可以使用该Marshal.SizeOf()方法,或sizeof在非托管代码中使用:

Console.WriteLine(Marshal.SizeOf(typeof(int)));

This prints 4on ideone.

这打印4在ideone上。

Here is a link to Eric Lippert's blogdescribing the difference between the two sizeof options.

这是Eric Lippert 博客链接,描述了两个 sizeof 选项之间的区别。

回答by xanatos

There are only a few standard situations where you'll want to do it:

只有几种标准情况您会想要这样做:

int x = sizeof(T) // where T is a generic type

sadly it doesn't work :-)

遗憾的是它不起作用:-)

int x = Marshal.SizeOf(T) // where T is a generic type

it does work except for charand bool(Marshal.SizeOf(typeof(char))== 1 instead of 2, Marshal.SizeOf(typeof(bool))== 4 instead of 1)

除了charand bool( Marshal.SizeOf(typeof(char))== 1 而不是 2, Marshal.SizeOf(typeof(bool))== 4 而不是 1 )

int x = sizeof(IntPtr);

it doesn't work, but you can do it as

它不起作用,但你可以这样做

int x = Marshal.SizeOf(typeof(IntPtr));

or, better

或更好

int x = IntPtr.Size;

All the other basic types (byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal, bool, char) have a fixed length, so you can do sizeof(int)and it will always be 4.

所有其他基本类型(byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal, bool, char)都有固定的长度,所以你可以这样做sizeof(int),它总是 4。

回答by moien

You can use sizeofon user-defined structs in unsafe contexts but unlike Marshal.SizeOf it does not support boxed objects

您可以sizeof在不安全的上下文中使用用户定义的结构,但与 Marshal.SizeOf 不同的是它不支持装箱对象