使用 null 初始化的 C# 值类型
在 C# 3.0 中可以将 null 赋给 int 吗? type(在CLR中int?是一个结构体):
int? a = null;
但是当你定义一个自定义结构体时:
struct MyStruct
{
}
在这段代码的编译过程中出现错误:
MyStruct a = null;
错误如下:
Cannot convert null to 'Example.MyStruct' because it is a non-nullable value type
而int?是 CLR 中的一个结构体,我们可以为其分配 null ,这在某种程度上是违反直觉的。 我认为 null 被隐式转换或装箱为某个 int ?表示空值的值。是如何精确完成的?如何以可以执行以下行的方式扩展 MyStruct:
MyStruct a = null;
In C# 3.0 you can assign null to int? type (in CLR int? is a struct):
int? a = null;
but when you define a custom struct:
struct MyStruct
{
}
there's an error during the compilation of this code:
MyStruct a = null;
The error is as follows:
Cannot convert null to 'Example.MyStruct' because it is a non-nullable value type
While int? is a struct in CLR it is somehow counterintuitive that we can assign null to it.
I suppose that null is implicitly casted or boxed to a certian int? value that represents the null value. How is it done precisely? How can I extend MyStruct in such a way that it would be possible to execute the line:
MyStruct a = null;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为
int?
实际上是简写Nullable
。您也可以对您的类型执行相同的操作:null
只能隐式转换为 nullable 类型,这意味着任何引用类型或任何可为 null 的值类型 - 后者对于某些T
意味着Nullable
。请注意,这种从
null
转换的能力实际上是一种语言功能 - 编译器将其转换为:它们
的含义相同 - 基本上是可空值的“null”值值类型是
HasValue
为 false 的值,默认情况下为 false。它与空引用不同 - 尽管像这样装箱空值将导致空引用。(这是跨越语言、库和 CLR 边界的功能的一个示例。库部分非常简单,CLR 部分仅与装箱和拆箱有关 - 但语言中的提升运算符等方面有很多内容.)
It's because
int?
is actually shorthandNullable<int>
. You can do the same thing for your type too:null
can only be implicitly converted to a nullable type, which means any reference type or any nullable value type - where the latter meansNullable<T>
for someT
.Note that this ability to conver from
null
is really a language feature - the compiler is converting this:into
They mean the same thing - basically a "null" value for a nullable value type is a value where
HasValue
is false, which it will be by default. It's not the same as a null reference - although boxing a null value like that will result in a null reference.(This is one example of a feature which crosses language, library and CLR boundaries. The library part is quite simple, the CLR part is only to do with boxing and unboxing - but there's quite a lot in terms of lifted operators etc in the language.)
结构体是 C# 中的值类型,而不是引用类型,因此它们不能为 null。
会起作用,但请记住结构是不可变的,例如:
Structs are value types in C#, not reference types, so they can't be null.
will work, but remember that structs are immutable, as a example:
只需添加即可,可为 null 的类型可以表示其基础值类型的正常值范围,再加上一个附加的 null 值。
在表示法中,就像 int 实际上是 Int32,而不是任何 int 一样,?符号表示可为空。
在本例中可以为空。
http://msdn.microsoft.com/en-us /library/1t3y8s4s%28VS.80%29.aspx
just adding, a nullable type can represent the normal range of values for its underlying value type, plus an additional null value.
In notation, just as int is actually Int32, not any int, the ? sign means a nullable.
Nullable, in this case.
http://msdn.microsoft.com/en-us/library/1t3y8s4s%28VS.80%29.aspx