I have this code:
List<List<string>> strs = new List<List<string>>();
List<string> str = new List<string>();
foreach (DataGridViewRow sr in dataGridView1.SelectedRows)
{
if(i >= 10)
{
strs.Add(str); //Here strs have one str with 10 values
str.Clear();
i = 0;
var a = strs; //Here strs have one str with 0 values
}
str.Add(sr.Cells["MOBILNI"].Value.ToString().Trim());
i++;
}
strs.Add(str);
I have list of strings which i populate and when it reach 10 members i put whole list into list of List<string>, then clear list of strings to populate it with next 10 of items. Problem is that when list of strings reach 10 members, i add it to list of List<string> and in debugging i see my strs has 1 member (List<string>) which has 10 elements (10 strings) but when i clear my base list of strings it also clears that list inside List<List<string>>.
When i use str = new List<string>() instead of str.Clear() it works normally.
So why is this happening and how to overcome it?