简体   繁体   English

为可空类型重载=运算符?

[英]Overloading the = operator for nullable types?

The MSDN mentions that overloading the = operator is not possible. MSDN提到不可能重载=运算符。

How is it possible then for Nullable types to be assigned to null? 那么如何将Nullable类型分配为null?

int? i = null;

Besides can I do it with my own generic types and how? 此外,我可以使用自己的通用类型来做到这一点吗?

It's the implicit-conversion not the assignment-operator that allows to assign null : http://msdn.microsoft.com/en-us/library/ms131346(v=vs.110).aspx 这是隐式转换,而不是允许分配null的赋值运算符: http : //msdn.microsoft.com/zh-cn/library/ms131346(v=vs.110).aspx

If the value parameter is not null , the Value property of the new Nullable<T> value is initialized to the value parameter and the HasValue property is initialized to true . 如果value参数不为null ,则将新Nullable<T>值的Value属性初始化为value参数,并将HasValue属性初始化为true If the value parameter is null , the Value property of the new Nullable<T> value is initialized to the default value , which is the value that is all binary zeroes, and the HasValue property is initialized to false. 如果value参数为null ,则将新Nullable<T>值的Value属性初始化为默认值 ,该值是全为二进制零的值,并且HasValue属性初始化为false。

Essentially what Tim's comment (Edit: And now answer =D) says - There's an implicit conversion from the null literal, rather than an overload of the assignment operator. 从本质上讲,蒂姆的评论(编辑:现在回答= D)说的是-从空文字中隐式转换,而不是赋值运算符的重载。

From the C# language spec (I was looking at Version 5.0) - Section "6.1.5 Null literal conversions" : 从C# 语言规范开始 (我当时在查看版本5.0)- "6.1.5 Null literal conversions"

An implicit conversion exists from the null literal to any nullable type. 存在从空文字到任何可空类型的隐式转换。 This conversion produces the null value (§4.1.10) of the given nullable type. 此转换将产生给定可空类型的空值(第4.1.1节)。

Nullable types are instances of the struct 可空类型是该结构的实例

System.Nullable<T>.

The type that can be specified or made nullable is specified as the generic type of nullable (T). 可以指定或可以为空的类型被指定为可空(T)的通用类型。

More info here... http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx 更多信息在这里... http://msdn.microsoft.com/zh-cn/library/1t3y8s4s.aspx

In your example, you're not actually setting an int to null, rather setting the value on the struct which encapsulates it to null. 在您的示例中,实际上并没有将int设置为null,而是将封装它的struct上的值设置为null。

There is special compiler support for the Nullable type. Nullable类型有特殊的编译器支持。

It is impossible to create a user-defined implicit conversion to/from null . 无法创建用户定义的null隐式转换或从null隐式转换。 They built it into the language (and the runtime) rather than creating Nullable on top of the language, as so many BCL classes are made. 他们将它内置到语言(和运行时)中,而不是在语言之上创建Nullable ,因为创建了许多BCL类。

Interestingly this is not the only special support created for Nullable . 有趣的是,这并不是为Nullable创建的唯一特殊支持。 When you box a Nullable<T> it doesn't actually box a Nullable object, ever . 当你框一个Nullable<T>实际上它并不箱一个Nullable对象, 直到永远 If HasValue is false, null is boxed, and if it's true, the underlying value is unwrapped and boxed. 如果HasValue为false,则将null装箱,如果为true,则将对基础值进行包装和装箱。 It would be impossible to do this for your own type. 对于您自己的类型,这是不可能的。

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

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