为什么C#结构不能返回对其成员字段的引用?

struct Foo { int i; public ref int I => ref i; } 

此代码引发编译错误CS8170,但如果Foo是类,则不会。 为什么结构不能返回成员作为参考?

我想我找到了解决办法:

 class Program { static void Main(string[] args) { Foo temp = new Foo(99); Console.WriteLine($"{Marshal.ReadInt32(temp.I)}"); Console.ReadLine(); } } struct Foo { int i; public IntPtr I; public Foo(int newInt) { i = newInt; I = GetByRef(i); } static unsafe private IntPtr GetByRef(int myI) { TypedReference tr = __makeref(myI); int* temp = &myI; IntPtr ptr = (IntPtr)temp; return ptr; } } 

这不是一个好主意 – 太多可怕的警告。 但是,我确实认为它是通过返回对struct成员的引用来实现你想要的,然后你可以编组以获得原始值。