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