please see these codes :
List<string[]> items_1 = new List<string[]>();
string[] item = new string[] { "item_id_for_sell", "item_title", "item_id_main", "item_amount", "item_del_id" };
items_1.Add(item);
item[0] = "1";
item[1] = "1";
item[2] = "1";
item[3] = "1";
item[4] = "1";
items_1.Add(item);
item[0] = "2";
item[1] = "2";
item[2] = "2";
item[3] = "2";
item[4] = "2";
items_1.Add(item);
i am so confused about this list.
why all string arrays inside that list have “2” value at last?
how can i prevent that list to update itself by updating item (string array)?
thanks in advance
You’re adding a reference to the same array three times, so changing the contents of the array will indeed allow that change to be seen however you get at it. It’s important that you understand that the list doesn’t contain the array itself – it contains references. See my article about reference types and value types for more information.
You probably just want a new array each time:
However, I’d urge you to reconsider whether an array of strings is really the best format here. Why not create an
Itemclass with propertiesId,Title,Main,AmountandDelId(whatever that means)? Then you can create aList<Item>which will be much easier to work with.