views:

477

answers:

3
List<string> list1 = new List<string>();
list1.Add("Blah");
list1.Add("Bleh");
list1.Add("Blih");

List<string> list2 = new List<string>();
list2.Add("Ooga");
list2.Add("Booga");
list2.Add("Wooga");

Is there a method to create a third list that has {"Blah", "Bleh", "Blih", "Ooga", "Booga", "Wooga"} or, alternatively, change list1 so it has the three additional elements in list2?

+8  A: 

I guess this is the solution:

list1.AddRange(list2)
Jenea
It can be done with List<T> but not with IList<T>.
TcKs
If IList<T> is used then I think Concat method can be used.
Jenea
@Jenea: my previous comment was note only. Your snippet works ofcourse :)
TcKs
Which is what I need, I'm using List<T>.
JCCyC
Oops :( I did see the other answer.
Jenea
+1  A: 

Take a look at the Union() method of a List.

Andrew Siemer
Union removes duplicated items, Concats don't.
TcKs
Note that Union will remove any duplicates; Concat may be preferred
Marc Gravell
+7  A: 

With LINQ, you can do:

List<string> list1 = new List<string>();
list1.Add("Blah");
list1.Add("Bleh");
list1.Add("Blih");

List<string> list2 = new List<string>();
list2.Add("Ooga");
list2.Add("Booga");
list2.Add("Wooga");

var finalList = list1.Concat( list2 ).ToList();
TcKs
+1 for the best answer. LINQ is the more powerful/flexible solution for handling IEnumerables.
Evan Plaice