Operator '||' cannot be applied to operands of type 'bool?' and 'bool?'


一开始想根据Button的Tag属性判断一些操作,于是写了

if (btn1.Tag.ToString().StartsWith("Menu") || btn2.Tag.ToString().StartsWith("Submenu"))
    return;

 

但是Tag属性可能为空,于是给Tag加上判空处理

if (btn1.Tag?.ToString().StartsWith("Menu") || btn2.Tag?.ToString().StartsWith("Submenu"))
    return;

 

这样又引发了语法错误。

Severity    Code    Description    Project    File    Line    Suppression State
Error    CS0019    Operator '||' cannot be applied to operands of type 'bool?' and 'bool?'

 

解决方案

if ((btn1.Tag?.ToString().StartsWith("Menu") ?? false) || (btn2.Tag?.ToString().StartsWith("Submenu") ?? false)) 
return;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM