c# - Safely combine two ILists, accounting for nulls -


i want combine 2 ilist's one, either of which, or both, null. if both null want result null.

i have want more elegant still easy read.

private ilist<filter> combinefilters(ilist<filter> first, ilist<filter> second) {     ilist<filter> result = null;      if(first != null)         if (second != null)             result = first.concat(second) ilist<filter>;         else             result = first;     else if (second != null)         result = second;      return result; } 

i think it’s bit of weird behavior if call combinefilters 2 lists (one possibly null) , list not copy. result in following behavior:

list<filter> filters1 = new list<filter>() { new filter() }; list<filter> filters2 = null;  console.writeline(filters1.count); // 1  var filters = combinefilters(filters1, filters2); filters.add(new filter());  console.writeline(filters.count); // 2 console.writeline(filters1.count); // 2 

i’m not sure if expect behavior.

so suggest ensure there new list in end. allows make short:

private ilist<filter> combinefilters(ilist<filter> first, ilist<filter> second) {     return (first ?? array.empty<filter>()).concat(second ?? array.empty<filter>()).tolist(); } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -