How do I change my new list without changing the original list?
You need to clone your list in your method, because List<T>
is a class, so it's reference-type and is passed by reference.
For example:
List<Item> SomeOperationFunction(List<Item> target)
{
List<Item> tmp = target.ToList();
tmp.RemoveAt(3);
return tmp;
}
Or
List<Item> SomeOperationFunction(List<Item> target)
{
List<Item> tmp = new List<Item>(target);
tmp.RemoveAt(3);
return tmp;
}
or
List<Item> SomeOperationFunction(List<Item> target)
{
List<Item> tmp = new List<Item>();
tmp.AddRange(target);
tmp.RemoveAt(3);
return tmp;
}
How can I append items to a list without changing the original list
Variables in Python are just references. I recommend making a copy by using a slice. l_copy = l_orig[:]
When I first saw the question (pre-edit), I didn't see any code, so I did not have the context. It looks like you're copying the reference to that row. (Meaning it actually points to the sub-lists in the original.)
new_list.append(row[:])
Change the copy of the list without changing the orginal one in Lisp
Copy the list with COPY-LIST
. The you can delete or add elements of the new list and the old list won't change.
Changes made in Python list showing up in copy of original list
Use copy.deepcopy
:
from copy import deepcopy
desc1 = deepcopy(desc)
list.copy
only makes a shallow copy: a new list
object, but its elements (here mutable sublists) will be references to the same objects as the elements of the original.
Changes made on copy list are reflecting original list - c#
The collection that you are referring to as 'Copy List' is not actually a Copy.
The elements inside the Collection are referring to the same Objects as in the Original Collection.
You will have to replace your copying foreach loop with something like this:
foreach (var item in forPrintKitchen)
{
forPrintKitchenOrders.Add(item.Clone()); // The clone method should return a new Instance after copying properties from item.
}
The Clone
method should create new
Instance and copy each of the properties from the instance being cloned and then return the newly created Instance.
Basically you'll have to define a method named Clone (name doesn't matter) in OrderTemp
class like this:
public class OrderTemp
{
/* other members of the class */
public OrderTemp Clone()
{
return new OrderTemp
{
property1 = this.property1;
property2 = this.property2;
/* and so on */
};
}
}
Related Topics
Serialization Breaks in .Net 4.5
"Padding Is Invalid and Cannot Be Removed" Using Aesmanaged
Exclude a Field/Property from the Database with Entity Framework 4 & Code-First
Displaying Tooltip on Mouse Hover of a Text
How to Draw a Rounded Rectangle in C#
Registry.Localmachine.Opensubkey() Returns Null
C# Form.Transparencykey Working Different for Different Colors, Why
How to Use Multi Color in Richtextbox
Combobox.Selectedtext Doesn't Give Me the Selectedtext
Non-Blocking Read from Standard I/O in C#
Openxml Sdk Having Borders for Cell
How to Convert Datetime in Specific Timezone
How to Sort a List<T> by Multiple T.Attributes
How to Change the Culture of a Winforms Application at Runtime
"Nested Foreach" VS "Lambda/Linq Query" Performance(Linq-To-Objects)