简体   繁体   English

Nullable怎么样? <T> 与类似的自定义C#结构不同?

[英]How is Nullable<T> different from a similar custom C# struct?

In Nullable micro-optimizations, part one , Eric mentions that Nullable<T> has a strange boxing behaviour that could not be achieved by a similar user-defined type. Nullable微优化(第一部分)中 ,Eric提到Nullable<T>具有奇怪的装箱行为 ,而这种行为无法通过类似的用户定义类型实现。

What are the special features that the C# language grants to the predefined Nullable<T> type? C#语言赋予预定义的Nullable<T>类型有哪些特殊功能? Especially the ones that could not be made to work on a MyNullable type. 特别是那些无法在MyNullable类型上工作的MyNullable

Of course, Nullable<T> has special syntactic sugar T? 当然, Nullable<T>有特殊的语法糖T? , but my question is more about semantics. ,但我的问题更多是关于语义。

What I was getting at is: there is no such thing as a boxed nullable . 我得到的是: 没有盒装可空的东西 When you box an int , you get a reference to a boxed int . 当你装入一个int ,你得到一个盒装int的引用。 When you box an int? 当你装一个int? , you get either a null reference or a reference to a boxed int . ,您获得空引用或对盒装int的引用。 You never get a boxed int? 你永远不会得到盒装int? .

You can easily make your own Optional<T> struct, but you can't implement a struct that has that boxing behaviour. 您可以轻松地创建自己的Optional<T>结构,但不能实现具有该装箱行为的结构。 Nullable<T> 's special behaviour is baked into the runtime. Nullable<T>的特殊行为被烘焙到运行时。

This fact leads to a number of oddities. 这一事实导致了许多奇怪的事情。 For example: 例如:

And FYI there are other ways in which the Nullable<T> type is "magical". 而且仅供参考, Nullable<T>类型还有其他方式是“神奇的”。 For instance, though it is a struct type, it does not satisfy the struct constraint. 例如,尽管它是结构类型,但它不满足结构约束。 There's no way for you to make your own struct that has that property. 您无法创建具有该属性的自己的结构。

I found these two in the C# specifications : 我在C#规范中找到了这两个:

  • The is operator works on T? is运营商在T? as it would have on T , and the as operator can convert to nullable types. 就像在Tas运算符可以转换为可空类型。
  • Predefined and user-defined operators that operate on non-nullable value types are lifted to the nullable forms of those types. 对非可空值类型进行操作的预定义和用户定义的运算符被提升为这些类型的可空形式。

Now, here are the features that I think are not limited to Nullable<T> : 现在,我认为这些功能不仅限于Nullable<T>

  • The value in a switch can be of a nullable type. switch的值可以是可空类型。 I don't think this counts, because switch also accepts user-defined implicit conversions that could be defined on a MyNullable type. 我认为这不重要,因为switch也接受可以在MyNullable类型上定义的用户定义的隐式转换。
  • Nullable IDisposable types are supported, with a null check being inserted before the generated calls to Dispose(). 支持Nullable IDisposable类型,在生成对Dispose()的调用之前插入空检查。 I don't think this counts, because I could define MyNullable as a class and then it would be the same. 我不认为这很重要,因为我可以将MyNullable定义为一个类,然后它将是相同的。

Here is what I am not sure about: 这是我不确定的:

  • The specs mentions boxing/unboxing and implicit/explicit conversions, but I do not understand whether the same results can be achieved with a MyNullable. 规范提到了装箱/拆箱和隐式/显式转换,但我不明白使用MyNullable是否可以实现相同的结果。

C# lifts operators on nullable types. C#提升运营商的可空类型。 For example: 例如:

int? SumNullableInts(int? a, int? b)
{
    return a + b;
}

You would have to do a lot of reflection work in MyNullable<T> to support that, and then the following would compile, where it shouldn't: 你必须在MyNullable<T>做很多反射工作来支持它,然后下面的代码将编译,它不应该:

MyNullable<List<string>.Enumerator> SumNullableEnumerators(MyNullable<List<string>.Enumerator> a, MyNullable<List<string>.Enumerator> b)
{
    return a + b;
}

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

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