none
Keeping objects in the original list unchanged RRS feed

  • Question

  • Hello,

    In my following code, myOriginalList is still getting modified. How do I keep it unchanged?

    Thanks for your help.

    List<Product> myOriginalList = GetList(); 
    List<Product> customList= new List<Product>();

                            foreach (var productType in myProductTypeList )
                            {
                                var tempList = myOriginalList.ToList(); 
                                var result = GetModifiedCustomList(tempList, productType );
                                customList.AddRange(result);                                                                                    
                            }
    .....
    .....
     private List<Product> GetModifiedCustomList(List<Product> inplist, string productType)
            {
                List<Product> tmpList = new List<Product>(inplist); 

                if (tmpList?.Count > 0)
                {
                    tmpList.ForEach(r => { if (r.ProductType == "NONE") { r.ProductType= productType; } }); 
                }
                return tmpList; 
            }  


    Wednesday, April 25, 2018 10:39 AM

Answers

  • If you implement IClonable interface in Product class then you can return IEnumerable<Product>. It is better than result List<T>. Then you will loop through inplist (why not inputList?) and if there is ProductType == "NONE" then you create clone object of product, set ProductType to productType and return it by yield return.
    • Proposed as answer by Fei HuModerator Thursday, April 26, 2018 8:13 AM
    • Marked as answer by ANi2000 Saturday, April 28, 2018 9:57 PM
    Wednesday, April 25, 2018 7:04 PM

All replies

  • You seem to have quite a bit of unnecessary list copying going on!

    E.g.

     var tempList = myOriginalList.ToList(); 
    List<Product> tmpList = new List<Product>(inplist); 

    Both may create copies of a list, but it is important to realise that these lists will contain references to the same objects.

    Therefore if you change a property of an object within one list, you will still see that change reflected in the other list (because they are the same object). This is because your Product objects are reference types.

    Have a read of Value types vs reference types.

    To create a complete copy of a list of objects you would also need to clone the elements in the list, that is make a clone (a copy) of each Product object in a new list rather than just copying the list.

    Wednesday, April 25, 2018 11:01 AM
  • If you implement IClonable interface in Product class then you can return IEnumerable<Product>. It is better than result List<T>. Then you will loop through inplist (why not inputList?) and if there is ProductType == "NONE" then you create clone object of product, set ProductType to productType and return it by yield return.
    • Proposed as answer by Fei HuModerator Thursday, April 26, 2018 8:13 AM
    • Marked as answer by ANi2000 Saturday, April 28, 2018 9:57 PM
    Wednesday, April 25, 2018 7:04 PM
  • Hello ANi2000,

    >>In my following code, myOriginalList is still getting modified. How do I keep it unchanged?

    The way you need to do is to deep clone OriginalList and then operate clone object. If you have implemented IClonable interface you could use extension method like below.

    static class Extensions
    {
        public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
        {
            return listToClone.Select(item => (T)item.Clone()).ToList();
        }
    }

    And if you don't implement ICloneable interface you could try below.

     List<Product> CloneList = myOriginalList.Select(product => new Product {id=product.id }).ToList();
    
    Assume product object as below.
     public class Product
        {
            public int id;
        }

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 26, 2018 8:25 AM
    Moderator
  • Thanks Neil and Petr. Yes, that worked!
    Saturday, April 28, 2018 9:58 PM