c# - 是整数吗?值类型还是引用类型?

标签 c# int value-type reference-type

<分区>

这个问题更多的是关于向值类型添加 ? 而不是 int?

在 C# 中,int 是一种值类型。 int? 是值类型还是引用类型?

在我看来它应该是一个引用类型,因为它可以是null

最佳答案

int?相当于 Nullable<int> 这意味着它是 struct .

所以这意味着它是一个 value type .

In my opinion it should be a reference type as it can be null.

你的假设是错误的。来自文档;

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个值;

  1. 数据的值(value)
  2. bool 值,确定值是否已设置。 ( Nullable<T>.HasValue )

关于c# - 是整数吗?值类型还是引用类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18743979/

相关文章:

c# - VS2008 单元测试 - 断言方法退出

c# - 使用 PDFsharp 连接 PDF 返回空 PDF

c - 仅使用 getchar() 从输入中获取整数并转换为 int

java - 如何检查 int 是否为偶数

c - int数组排序修改数组内容

.net - 如何区分一个类型是 ValueType 还是 RefereceType?

c# - .NET 值类型在内存中的布局

c# - 从 C# Windows 窗体调用 PHP Web 服务

c# - 在 MySQL 数据库中存储 List<T> 数组