相关疑难解决方法(0)

sizeof()结构未知.为什么?

为什么我不能在简单的结构上使用sizeof()?

例如:

private struct FloatShortPair
{
    public float myFloat;
    public short myShort;
};

int size = sizeof(FloatShortPair);  //CS0233
Run Code Online (Sandbox Code Playgroud)

错误CS0233:'FloatShortPair'没有预定义的大小,因此sizeof只能在不安全的上下文中使用(考虑使用System.Runtime.InteropServices.Marshal.SizeOf)

MSDN声明:

sizeof运算符只能用于编译时常量的类型.如果收到此错误,请确保可以在编译时确定标识符的大小.如果不能,则使用SizeOf而不是sizeof.

float和short如何编译时间常量?8 /

c# struct sizeof

23
推荐指数
1
解决办法
5752
查看次数

CLR顺序结构布局:对齐和大小

struct默认情况下,C#中的所有内容都被视为已[StructLayout(LayoutKind.Sequential)]标记的值类型.所以我们拿一些structs来检查这个structs的大小:

using System;
using System.Reflection;
using System.Linq;
using System.Runtime.InteropServices;

class Foo
{
  struct E { }
  struct S0 { byte a; }
  struct S1 { byte a; byte b; }
  struct S2 { byte a; byte b; byte c; }
  struct S3 { byte a; int b; }
  struct S4 { int a; byte b; }
  struct S5 { byte a; byte b; int c; }
  struct S6 { byte a; int b; …
Run Code Online (Sandbox Code Playgroud)

.net c# clr struct value-type

5
推荐指数
1
解决办法
1373
查看次数

标签 统计

c# ×2

struct ×2

.net ×1

clr ×1

sizeof ×1

value-type ×1