c# - 在c#中将可空字符串转换为可空int
问题描述
我有下面的变量
int? a=null ;
string? b= null;
我需要分配 a=b ;
在 c# 9 中分配的最佳方法是什么
a= Convert.ToInt32(b);
它分配 0 ..hw 来分配 null 如果字符串也为 null .. 我需要在 c# 9 中知道
编辑:感谢@john ..我最终得到以下代码
if(b is not null)
a = Convert.ToInt32(b);
解决方案
我会非常明确地说明这一点:
int? a = b is null ? null : Convert.ToInt32(b);
推荐阅读
- swift - 如何在故事板中创建的 UILabel 上换行文本?
- excel - 如果输入的时间段多于 EXCEL-VBA 中的特定值,如何复制最后一个特定数量的值
- laravel - laravel 8 ->为具有多态关系的帖子添加评论和回复
- concurrency - 不同的并发范式如何比较?
- docker - Windows 10 上的 Docker 绑定 0.0.0.0:80 失败:端口已分配
- recursion - 在 Kusto Azure 数据资源管理器中构建树/运行递归查询
- php - $_SESSION 始终为空
- macos - NSItemProvider 在 SwiftUI MacOS 应用程序中没有注册类型标识符
- java - Android 读写 LocalDate 到 SharedPreferences 的麻烦
- web-scraping - 使用请求时出现 403 禁止错误,即使在 curseforge 上将标头设置为 {'User-Agent': 'Mozilla/5.0'}