Sooner or later, you will encounter a scenario where you need to combine the elements of two list objects into a new list.

Take the following case:

List<string> currentNames = ["Brenda", "Latisha", "Linda", "Felicia", "Dawn", "LeShaun", "Ines", "Alicia"];
List<string> newNames = ["LeShaun", "Ines", "Alicia", "Teresa", "Monica", "Sharon", "Nicki", "Lisa", "Veronica", "Karen", "Vicky"];

We have two lists, currentNames, and newNames, and we want to combine them.

The usual go-to is the LINQ union method.

var newList = currentNames.Union(newNames).ToList();

There is also the Concat method that you can use.

var newList = currentNames.Union(newNames).ToList();

They seem interchangeable, but they aren’t.

If we run this code:

Console.WriteLine($"newList has {newList.Count} elements, and otherNewList has {otherNewList.Count}");

We get the following results:

joinLists

Why do they have different lengths?

Because union is based on set theory, and does not allow duplicates.

Concat, on the other hand, has no such restrictions. Which means it has the additional benefit of being faster, as it does not need to check for duplicates.

The choice of which to use depends on your requirements.

This code will also work for arrays and most of the other collections.

TLDR

You can use union or concat to combine two collections, depending on your needs.

Happy hacking!