How To Merge / Join Two Typed Lists



TODO:

Have you ever wanted to combine 2 typed lists in C#?

 

SOLUTION:

 

string data = "test,test2,test3";
string data2 = "test4;test5;test6";

List<string> list1 = data.Split(',').ToList<string>();
List<string> list2 = data2.Split(';').ToList<string>();
list1 = list1.Union(list2).ToList();

 

 

NOTES:

The example above is one that I use when I want to allow a comma or semi-colon delimited list.  For instance in my config file, people sometimes use a comma to seperate email addresses, and other times they use a semi-colon.  This will split the data one both, and join the 2 lists.