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

前端 未结 9 716
陌清茗
陌清茗 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:58

    I'm facing your question when I'm using the null check operator ?.:

    if (!RolesList?.Any()) //Not accepted: cannot convert bool? to bool
    

    So I'm using this instead

    if (RolesList?.Any() != true)
      //value is null or false
    

    In your case you should set it like so:

    obj.IsVisible = chkDisplayStuff.IsChecked ?? false;
    

提交回复
热议问题