我想將兩個IList
合併成一個,其中之一或兩者都可以是null
。如果兩者都是null
我希望結果爲null
。安全地結合兩個IList,佔空位
我有這個,但我想要更優雅的東西,但仍然容易閱讀。
private IList<Filter> CombineFilters(IList<Filter> first, IList<Filter> second)
{
IList<Filter> result = null;
if(first != null)
if (second != null)
result = first.Concat(second) as IList<Filter>;
else
result = first;
else if (second != null)
result = second;
return result;
}
可能會更好地詢問代碼審查,因爲它似乎是工作代碼。我可能會問,爲什麼你試圖避免將'null'分配給'result',如果事實證明這是正確的結果(即爲什麼'else if')? –
你需要'first.Concat(second).ToList()'而不是'as IList'否則結果將爲空 –
@Damien_The_Unbeliever'else if'發生在'first'爲'null'和'second '不是 – Bobbler