I have a list of lists that I need to "copy" and do some modification on the copied object. However, the updates that I'm doing on the target object seems to be updating the source object as well because of the reference.
I'm aware in order to "clone" my source list I have to do .ToList()
. However, this is not working in my case. All I want is to update my target without affecting the source. Any help is greatly appreciated
static void Main(string[] args)
{
List<List<string>> list = new List<List<string>>();
list.Add(new List<string> { "abc", "def" });
CreateList(list);
}
static void CreateList(List<List<string>> original)
{
List<List<string>> temp = original;
temp.ToList();
foreach (var row in temp)
{
row.Add("zzz"); //this adds it in the "original" object as well along with "temp"
}
}