问题
My code looks like this.
public class Category
{
public string CatId { get; set; }
public string CatName { get; set; }
public string SpecId { get; set; }
public string SpecName { get; set; }
}
List<Category> LstCategory = new List<Category>();
List<Category> LstCatWithSpec = new List<Category>();
Now, I want to add category first in LstCatWithSpec
. I copy this list in LstCategory
and then add specialties in LstCatWithSpec
. Now, when I update list with specialties, the list in which I copied only categories is also changed. It is also filled with specialties. It should not change right ?
How is this possible when I have two different objects and I give specific object name when I am adding items to it ?
As I am working on xamarin.forms
, I have tagged xamarin.forms
because I don't know if it exists in c#
or not.
EDIT -
I tried to add list using foreach
loop.
foreach( var item in LstCatWithSpec)
{
LstCategory.Add(item);
}
And I tried this also.
LstCategory = new List<Category>(LstCatWithSpec);
来源:https://stackoverflow.com/questions/40573715/list-is-changed-when-i-update-another-list-with-same-data-type