gpt4 book ai didi

c# - 在c#中将可为空的字符串转换为可为空的int

转载 作者:行者123 更新时间:2023-12-03 18:39:33 31 4
gpt4 key购买 nike

我有下面的变量

  int? a=null ;

string? b= null;
我需要分配 a=b ;
在 c# 9 中分配的最佳方式是什么
a= Convert.ToInt32(b);
如果字符串也为空,则分配 0 ..hw 以分配空。我需要在 c# 9 中知道
编辑:感谢@john .. 我最终得到了以下代码
  if(b is not null) 
a = Convert.ToInt32(b);

最佳答案

我会非常明确地说明这一点:

int? a = b is null ? null : Convert.ToInt32(b);

关于c# - 在c#中将可为空的字符串转换为可为空的int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65368473/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com