Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, June 9, 2010 1:05 PM
I have an IList<string> that I want to append to another IList<string>. Is there a way to do this without iterating through the list and using Add() ?
All replies (6)
Wednesday, June 9, 2010 1:16 PM ✅Answered
Hi,
Will this help?
IList<string> oIList1 = new List<string>{"1","2","3"};
IList<string> oIList2 = new List<string>{"4","5","6"};
IList<string> oIList3 = oIList1.Concat(oIList2).ToList();
Regards,
Vinil;
Wednesday, June 9, 2010 3:21 PM ✅Answered
Hi, use Addrange or new List<T>(yourIlist)
Wednesday, June 9, 2010 1:18 PM
That depend of how is your implementation of a collection (I assume IList is not a misspelling). If you are using a linked list that has a reference to the last node (normally used in doble linked list) then is fine you only have to link the last node of one to the first node of the other, if not then yes there is no other way to merge two linked lists.
About the answer listed above, perhaps I'm overthinking sorry if I am, but I don't remenber the default implementation of List but I believe is a generic arraylist so merging two List will be the same as merging two arrays. So using Concat or AddRange will do the operation with the annoying part of resizing the array if out of bounds, if you use a linked list then you need to iterate to the last node or use a reference to the last node.
Now I don't know for sure what c# use in the back end (look it up) but if not you can create your own collection or define your own methods of adding two list
Wednesday, June 9, 2010 2:32 PM
// Source list
IList<string> list1 = new List<string>(new[] {
"one", "two"
});
// Target list
// Use the constructor to pass the source list.
IList<string> list21 = new List<string>(list1);
// Or cast it to 'List' (if it's a 'List') and use '.AddRange(list1)'
List<string> list22 = (List<string>)list21;
list22.AddRange(list1);
Eyal, Regards.
Wednesday, June 9, 2010 2:47 PM
// Source list IList<string> list1 = new List<string>(new[] { "one", "two" }); // Target list // Use the constructor to pass the source list. IList<string> list21 = new List<string>(list1); // Or cast it to 'List' (if it's a 'List') and use '.AddRange(list1)' List<string> list22 = (List<string>)list21; list22.AddRange(list1);
Eyal, Regards.
You cheated - you did what I did and left the interface for a typed instance. :)
Just a quick note - your code is converting it back to List. Make sure it's casting:
List<string> list22 = list21 as List<string>;
Wednesday, June 9, 2010 11:23 PM
You cheated - you did what I did and left the interface for a typed instance. :)
Haha .. :D
Just a quick note - your code is converting it back to List. Make sure it's casting:
List<string > list22 = list21 as List<string >;
Yeah, I should have, although it was merely an example. :p
Eyal, Regards.