Combining 2 Generic Lists into 1 with C#

I was writing a program that converted a List into a comma delimited string. A later requirement necessitated that I capture data from an additional source and add it to the original comma delimited string. In both cases the data was returned in a generic list, so I only needed to combine the 2 lists into 1 before converting it to a delimited string. In this case, order and uniqueness was not a requirement. It is quit easy to do this.

First I had my 2 generic lists:

[sourcecode language="csharp" padlinenumbers="true" autolinks="false" gutter="false" toolbar="false"]
public static List<string> First = new List<string>();
public static List<string> Second = new List<string>();
[/sourcecode]

I populated both of them with the data and then used the AddRange() method to merge the 2 together.

[sourcecode language="csharp" autolinks="false" gutter="false" toolbar="false"]
First.AddRange(Second);
[/sourcecode]

Download the source.




Leave a Comment

Your email address will not be published.