IEnumerable<T>.Concat -- A replacement that can work without changing the reference?
Posted
by Earlz
on Stack Overflow
See other posts from Stack Overflow
or by Earlz
Published on 2010-03-28T00:25:09Z
Indexed on
2010/03/28
0:33 UTC
Read the original article
Hit count: 587
Hello, I've recently been bitten by the (way too commmon in my opinion) gotcha of Concat
returns it's result, rather than appending to the list itself.
For instance.
List<Control> mylist=new List<Control>;
//.... after adding Controls into mylist
MyPanel.Controls.Concat(mylist); //This will not affect MyPanel.Controls at all.
MyPanel.Controls=MyPanel.Controls.Concat(mylist); //This is what is needed, but the Controls reference can not be reassigned (for good reason)
So is there some other way of combining two lists that will work when the collection reference is read-only?
Is the only way to do this with a foreach?
foreach(var item in mylist){
MyPanel.Controls.Add(item);
}
Is there a better way without the foreach?
© Stack Overflow or respective owner