【问题标题】:Convert nullable string to nullable int in c#在c#中将可空字符串转换为可空int
【发布时间】:2020-12-19 09:42:58
【问题描述】:

我有下面的变量

  int? a=null ;
    
  string? b= null;

我需要分配 a=b ;

在 c# 9 中分配的最佳方法是什么

a= Convert.ToInt32(b);

如果字符串也为空,则分配 0 ..hw 以分配 null.. 我需要在 c# 9 中知道

编辑:感谢@john..我最终得到了以下代码

  if(b is not null) 
     a = Convert.ToInt32(b); 

【问题讨论】:

    标签: c# c#-9.0


    【解决方案1】:

    我只想说清楚:

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

    【讨论】:

      猜你喜欢
      • 2014-05-25
      • 2014-03-02
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多