C# 7.2引入了 ref struct
s。但是,给定一个ref struct
这样的:
public ref struct Foo {
public int Bar;
}
我不能将它用作类型参数:
int i = 0;
var x = Unsafe.As<int, Foo>(ref i); // <- Error CS0306 The type 'Foo' may not be used as a type argument.
我知道 ref 结构只能存在于堆栈上,而不能存在于堆上。但是,如果保证使用此类 ref 结构的泛型方法永远不会将它们放在堆上,就像上面使用System.Runtime.CompilerServices.Unsafe
包的示例中那样?为什么在这些情况下我不能将它们用作类型参数?