cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

前端 未结 9 712
陌清茗
陌清茗 2020-12-05 06:14

Error : cannot implicitly convert type \'bool?\' to \'bool\'. An explicit conversion exists (are you missing a cast?)

Code :



        
9条回答
  •  悲&欢浪女
    2020-12-05 06:55

    chkDisplay.IsChecked is of type bool?. Which means it can hold values true, false and null. However, obj.IsDisplay is of type bool. Which means it can only hold true or false.

    Hence you have to explicitly cast it to type bool. However, this will still throw an exception if, the value you are trying to cast to bool is null.

    bool? nullableBool = null;
    bool notNullableBool = (bool)nullableBool; //This will throw InvalidOperationException
    

提交回复
热议问题