How to assign List<T> without it being a reference to the original List<T>?

c#, reference

Solution

name_list2 = new List<string>(name_list1);

This will clone the list.

Edit: This solution only works for primitive types. For objects, see other responses below.

Problem

For example ``` List<string> name_list1 = new List<string>(); List<string> name_list2 = new List<string>(); ``` later in the code: ``` name_list1.Add("McDonald"); name_list1.Add("Harveys"); name_list1.Add("Wendys"); name_list2 = name_list1; // I make a copy of namelist1 to namelist2 ``` So, from this point I would like to keep adding element or making changes in name_list2 without affecting name_list1. How do I do that?

Original source

Related problems