是int吗?值类型或引用类型? [英] Is int? a value type or a reference type?
问题描述
这个问题更多是关于将?
添加到大于 int的值类型?
This question is more about adding a ?
to a value type than about int?
在C#中, int
是一种值类型。
是 int?
值类型还是引用类型?
In C# an int
is a value type.
Is int?
a value type or a reference type?
在我看来它应该是一个引用类型,因为它可以是 null
。
In my opinion it should be a reference type as it can be null
.
推荐答案
int?
相当于 Nullable< int>
这意味着它是 struct 。
这意味着它是值类型。
在我看来它应该是一个引用类型,因为它可以为null。
In my opinion it should be a reference type as it can be null.
你的假设是错误的。来自文档;
Your assumption is wrong. From documentation;
Nullable结构表示可以为分配
null 的值类型。 Nullable结构支持仅使用值类型作为
a nullable类型,因为引用类型可以通过设计为空。
Nullable structure represents a value type that can be assigned null. The Nullable structure supports using only a value type as a nullable type because reference types are nullable by design.
所以它有2个值;
- 数据的值
- 确定值已设置的布尔值或不。 (
Nullable< T> .HasValue
)
这篇关于是int吗?值类型或引用类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!