unwanted object property changed when changing another object property c#

人走茶凉 提交于 2019-12-31 04:58:07

问题


from what i understand in other posts i know that my objects use same place in memory but how to separate these objects? i tried to use new but it didn't work or i didn't used it correctly.

Note that i didnt paste setter and getter here.

class Supermarket
{
    List<Product> _products = new List<Product>{ };
    List<Customer> _customers = new List<Customer>{ };
}

class Customer
{
    List<Product> _purchased= new List<Product>{ };
}
class Product
{
    string _id;
    string _name;
    DateTime _expireDate;
    int _cost;
    int _count;
}

i add one Product in one Method.

Product product = new Product(...);
supermarket.Products.Add(product);

and in another method i want to copy the Product from Supermarket.Products to Supermarket.Customers.Purchased. so i want a copy but i cant get it.

here i want to make Copy but it does not work.

Product product = supermarket.Products[productIndex];
supermarket.Customers[customerIndex].Purchased.Add(product);

now the problem is when i change Product properties in Customer class , Product properties inside Supermarket will change too. for example

supermarket.Customers[customerIndex].Purchased.Last().Count = ...
//now the Product supermarket.Products[productIndex] will change too witch is unwanted

回答1:


The reason why it is not working is because you are doing shallow copy by just adding the pointer of product object into the list, not all properties. So if you change one, another will be affected accordingly.

You can use deep copy following this answer, but this way you have to mark your class as [Serializable]. The simplest way which I think is to use Json serializer:

public static class CloneHelper 
{
    public static T Clone<T>(T source)
    {
        var serialized = JsonConvert.SerializeObject(source);
        return JsonConvert.DeserializeObject<T>(serialized);
    }
}

var copyProduct = CloneHelper.Clone<Product>(product);

Or simply, you can manage by yourself as the below code, then it works:

Product product = supermarket.Products[productIndex];

Product copyProduct = new Product() {
    Id = product.Id,
    Name = product.Name,
    ExpireDate = product.ExpireDate,
    Cost = product.Cost,
    Count = product.Count   
};

supermarket.Customers[customerIndex].Purchased.Add(copyProduct);


来源:https://stackoverflow.com/questions/30444315/unwanted-object-property-changed-when-changing-another-object-property-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!