Compiler Error CS0233
“identifier”没有预定义大小,因此 sizeof 只能用于不安全的上下文中(请考虑使用 System.Runtime.InteropServices.Marshal.SizeOf)
sizeof 运算符只能用于为编译时常数的类型。如果您遇到此错误,请确保在编译时可以确定标识符的大小。如果不能确定,则用 SizeOf 代替 sizeof。
下面的示例生成 CS0233:
// CS0233.cs
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct S
{
public int a;
}
public class MyClass
{
public static void Main()
{
S myS = new S();
Console.WriteLine(sizeof(S)); // CS0233
// Try the following line instead:
// Console.WriteLine(Marshal.SizeOf(myS));
}
}