考虑以下类型:
-
(int, int)
管理好了。
-
struct MyStruct { public (int,int) Value; }
未管理!
问题:
非通用结构
MyStruct
,它有一个托管成员
(int,int)
已被评估为托管类型。
预期行为:
包含托管成员的结构应被视为托管,与
struct MyStruct { int? Value; }
被认为是有管理的。
似乎这两种类型都违反了文件
[1]
和
[2]
.
示例1-非托管约束
class Program
{
static void DoSomething<T>() where T : unmanaged { }
struct MyStruct { public (int, int) Value; }
static void Main(string[] args)
{
DoSomething<MyStruct>(); // â OK
DoSomething<(int, int)>(); // â Shows compile-time error
}
}
错误CS8377类型“(int,int)”必须是不可为空的值类型,
以及任何嵌套级别的所有字段,以便将其用作
泛型类型或方法“program.dosomething()”中的参数“t”
示例2-指针或sizeof
使用上面的结构,指针或
sizeof
操作员:
unsafe
{
(int, int)* p1; // â Compile-time error,
MyStruct* p2; // â Compiles
}
错误CS0208无法获取的地址、大小或声明
指向托管类型(“(int,int)”的指针
问题
-
结构如何包含
ValueTuple
被认为是
unmanaged
并能满足
非受管的
当
价值观
是否被视为管理?
-
结构如何具有
ValueTupple<T1, T2>
以及包含
Nullable<T>
有不同的待遇吗?
注1:
在我看来,这个问题不同于
Proposal: Unmanaged constructed types
(大卫在评论中提到),因为
MyStult
不是一般的,另一方面,
int?
和
(int,int)
两者都有管理,但是
结构mystruct int?价值;
和
struct MyStruct { (int, int) Value; }
评价不同。