c# - 如何在c#中将值类型设置为null?
问题描述
我目前正在尝试设置一个为空的 int,类似于字符串可以为空的方式。我试过: int i = null; 返回Cannot convert null to 'int' because it is a non-nullable value type
但字符串 s = null; 完全没问题。
解决方案
因此默认情况下值类型不能设置为 null,但是有一些方法可以将它们设置为 null。要解决您的问题,您需要这样做:
int? i = null;
这来自此处的 Microsoft 文档: https ://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/
值类型与引用类型可以在这里找到: https ://www.tutorialsteacher.com/csharp/csharp-value-type-and-reference-type
推荐阅读
- python - 由于 CORS 问题,Angular 应用程序无法读取 Django Rest Framework API
- if-statement - Jenkins Pipeline if-else 脚本没有给出预期结果
- php - 安装根目录上的 WordPress qw.php 文件
- excel - 将特定文本从一个工作表复制到另一个工作表
- java - 在 Java 中使用 AES256 加密的字符串很少
- javascript - 如何在angularjs中单击按钮时获得选定的值?
- reactjs - 类型错误未定义,设置状态时收到此错误不太清楚为什么
- fortran - 如何用do循环形成人物设计
- python - 循环可以在全局框架内引用列表而不命名它吗?
- node.js - RequestError:无效的列名节点js sql server护照