【问题标题】:C# How to insert items at specific index in nullable bool ListC#如何在可为空的布尔列表中的特定索引处插入项目
【发布时间】:2016-03-20 08:19:21
【问题描述】:

我有List<bool> bools,我想在它的特定索引处插入一些 bool 变量,但我也想将一些索引设置为 null 或与 true/false 不同的东西,但由于它是一个 bool 列表,它不能是一个字符串我想到的第一个是空的。一旦我将其设置为可空的List<bool> ?bools,我就无法再使用bools.Insert(index,variable); 那么我该怎么做呢?

  • 我想把索引放在那里我不想完全删除它

【问题讨论】:

  • 这甚至不应该编译,因为 List<bool>? 将转换为 Nullable<List<bool>>Nullable 仅适用于不可为空的类型,而 List<bool> 是可空的类型。

标签: c# list null boolean


【解决方案1】:

尝试使用List<bool?> bools

【讨论】:

    【解决方案2】:

    您应该声明您的列表为List<bool?>

    var bools = new List<bool?>();
    bools.Insert(9,false);
    

    请注意,它是.Insert(index,value),而不是相反。

    【讨论】:

      猜你喜欢
      • 2019-10-09
      • 2020-07-24
      • 1970-01-01
      • 2023-03-21
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      相关资源
      最近更新 更多