简体   繁体   English

关于Nullable <T>约束的困惑

[英]Confusion about Nullable<T> constraints

Greetings everybody. 向大家致以问候。 I am sorry, if this was already asked before (searched in vain) or is really very simple, but i just can't get it. 对不起,如果以前已经问过这个问题(徒劳无功),或者说非常简单,但我无法得到它。 The MSDN definition of a Nullable type, states, that it is defined in a following manner: Nullable类型的MSDN定义声明它是以下列方式定义的:

[SerializableAttribute]
public struct Nullable<T>
where T : struct, new()

So the question is quite straightforward: How is this definition possible? 所以问题非常简单:这个定义怎么可能? Or this is just a typo? 或者这只是一个错字? Every value type already has a default constructor. 每个值类型都有一个默认构造函数。 Indeed, when i try to compile something like this, the compiler reasonably says, that it is illegal to apply both constraints at the same time, because the second one is implicitly included in a first one. 实际上,当我尝试编译这样的东西时,编译器合理地说,同时应用两个约束是非法的,因为第二个隐含地包含在第一个约束中。

Thanks in advance. 提前致谢。

I think it's just a mistake in the documentation. 我认为这只是文档中的一个错误。 If you look at the Nullable<T> type in Reflector or using the "Go To Definition" command in VS, it shows only the struct constraint. 如果在Reflector中查看Nullable<T>类型或在VS中使用“Go To Definition”命令,它只显示struct约束。


EDIT 编辑

I was thinking again about that, and I did a little test : 我正在考虑这个问题,我做了一点测试:

var attributes = typeof(Nullable<>).GetGenericArguments()[0].GenericParameterAttributes;
Console.WriteLine(attributes);

This code produces the following output : 此代码生成以下输出:

NotNullableValueTypeConstraint, DefaultConstructorConstraint NotNullableValueTypeConstraint,DefaultConstructorConstraint

So according to reflection, the T in Nullable<T> does have the new() constraint... Which means that even though it's invalid in C#, it must be valid for the CLR. 因此,根据反映, TNullable<T> 确实new()的约束......这意味着,即使它在C#中无效的,它必须是有效的CLR。

So the documentation is both right and wrong : it's true that the T in Nullable<T> has the "default constructor" constraint, but the C# declaration it shows is wrong... 所以文档是正确的和错误的: Nullable<T>中的T确实具有“默认构造函数”约束,但它显示的C#声明是错误的......

This is actually not very surprising, since the documentation is generated from assembly metadata (and XML comments of course). 这实际上并不令人惊讶,因为文档是从程序集元数据(当然还有XML注释)生成的。 There must be a bug in the documentation generator... 文档生成器中必定存在错误...

C# requires that value types have default public constructors, but the CLR does not. C#要求值类型具有默认的公共构造函数,但CLR不需要。

If you defined a type in a language that supported a struct definition of this type (I believe C++/CLI allows this) there would be ambiguity as to when it is called. 如果您使用支持此类型的结构定义的语言定义了一个类型(我相信C ++ / CLI允许这样做),那么调用它的时间就会很模糊。

If you look at the IL disassembly, you can see that it does have a constructor constraint: 如果你看一下IL反汇编,你会发现它确实有一个构造函数约束:

.class public sequential ansi serializable sealed beforefieldinit Nullable<valuetype (System.ValueType) .ctor T>

If the documentation is generated directly from the assembly metadata, maybe this is the cause? 如果文档是直接从程序集元数据生成的,那么这可能是原因吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM