bool CheckIfAvailable(HashSet<int> hayStack1, HashSet<int> hayStack2, int needle)
{
bool? s1 = hayStack1?.Contains(needle);
bool? s2 = hayStack2?.Contains(needle);
if (s1 ?? s2 ?? true) return true;
return s2 == true;
}
說明:
if (s1 ?? s2 ?? true)
只爲真時:
s1 == s2 == null
或者s1 == true || (s1 == null && s2 == true)
return s2 == true;
由於s1
可能會變成錯誤,我們檢查s2
是否爲真。
可以用單個return語句替換它。
bool CheckIfAvailable(HashSet<int> hayStack1, HashSet<int> hayStack2, int needle)
{
bool? s1 = hayStack1?.Contains(needle);
bool? s2 = hayStack2?.Contains(needle);
return (s1 ?? s2 ?? true) || s2 == true;
}
這是一個乾淨的方法謝謝 – m2008m1033m
很好。你能否進一步解釋爲什麼'返回s2 == true;'是必要的?它與併發有關嗎? –
@TadijaBagarić這是針對s1 == false和s2 == true的情況。 – m2008m1033m