Nullable Type如何能够使值为null,即使它是Type为“STRUCT”的类型也是如此。 [英] How Nullable Type is able to null value even when it is of Type "STRUCT"

查看:102
本文介绍了Nullable Type如何能够使值为null,即使它是Type为“STRUCT”的类型也是如此。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道我们无法将 Null 值分配给 Struct 类型,因此我们不会将 null 值赋给int因为它的类型为 Struct 。因此,我们有 Nullables ,它可以存储 null 值,但 Nullable 本身就是结构 ,那么我们如何能够为它分配 Null

解决方案

来自MSDN:

http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx [ ^ ]



备注部分:

Quote:

Nullable类提供补充支持对于Nullable< t>结构体。 Nullable类支持获取可空类型的基础类型,以及对可空类型对的比较和相等操作,其基础值类型不支持通用比较和相等操作。



基本属性

Nullable< t>的两个基本成员。结构是HasValue和Value属性。如果Nullable< t>的HasValue属性对象为true,可以使用Value属性访问对象的值。如果HasValue属性为false,则对象的值未定义,并且尝试访问Value属性会引发InvalidOperationException。


As we know that we cannot assign "Null" values to "Struct" type, therefore we do not assign null value to int since it is of type "Struct". Therefore we have "Nullables" which can store "null" values but "Nullable" are itself a "Struct", so how are we able to assign "Null" to it.

解决方案

From MSDN:
http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx[^]

in the Remarks section:

Quote:

The Nullable class provides complementary support for the Nullable<t> structure. The Nullable class supports obtaining the underlying type of a nullable type, and comparison and equality operations on pairs of nullable types whose underlying value type does not support generic comparison and equality operations.

Fundamental Properties
The two fundamental members of the Nullable<t> structure are the HasValue and Value properties. If the HasValue property for a Nullable<t> object is true, the value of the object can be accessed with the Value property. If the HasValue property is false, the value of the object is undefined and an attempt to access the Value property throws an InvalidOperationException.


这篇关于Nullable Type如何能够使值为null,即使它是Type为“STRUCT”的类型也是如此。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆