0

I have problem with cloning List item.

Class Item
{
String Name;
Int Age;
}

Than I have a List<>

List<Item> Items = new List<Items>();

now if i do this action

Item val= Items[i];

Now if I change val's age,it will also change Items[i] age. How to clone item from List,so when changing values in cloned item,values wouldn't change in List? This if for examle,but my class im bigger,it contains more than 20 values.

  • @CircleHsiao He's asking about **one item** not the whole `List<>` – mrogal.ski Jan 05 '17 at 09:15
  • m.rogalaski,that's right.I need to get only one item from List. – Luka Berianidze Jan 05 '17 at 09:16
  • "Clone correctly" is such a lose specification. For your **specific** type, you will need to construct a new `Item` and copy over values. For other types the answer may be different because you haven't specified if you want a shallow or a deep clone, or a mix of shallow and deep depending on the data. – Lasse V. Karlsen Jan 05 '17 at 09:20
  • The best way is to either implement an interface on your type, delegating to the type exactly how it duplicates itself, or to a separate factory/cloner class that knows how to duplicate the specific types in a specific context. – Lasse V. Karlsen Jan 05 '17 at 09:21

2 Answers2

0

A Class is a reference type. By using Item val= Items[i]; you're just assigning the reference to Items[i] to val, so when you amend one you amend the other.

You need to create a new Item and populate its properties from the original Item[i]:

var val = new Item()
{
    Name = Items[i].Name,
    Age = Items[i].Age
};

It's possible you may need to set the correct access on your Item class as well:

public class Item
{
    public string Name { get; set; }
    public int Age { get; set; }
}
Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • I know.But i have class with 20+ values in it.So its very hard to clone like this. – Luka Berianidze Jan 05 '17 at 09:20
  • 1
    Well maybe that's some information you should put in your question... – Equalsk Jan 05 '17 at 09:21
  • 1
    We can only answer the question you posted, not the one you *should've* posted. The "best" method to clone a type with 2 properties may be different from cloning a type with 20+ or 200+ properties. It is good of you to post an example of what you want, but please also post some specifics about the actual scenario you will apply the solution to. For instance, what if your type has properties with other objects in them, do you want to clone them as well (deep clone) or do you want to copy them over, just clone the initial level of properties (shallow clone)? – Lasse V. Karlsen Jan 05 '17 at 09:25
  • I can't understand what do you mean in Shallow and deep clone,but i can say i need that after cloning cloned item interrupts all communications with source item. – Luka Berianidze Jan 05 '17 at 09:35
0
Item it = new Item();
it.Name = Items[i].Name;
it.Age = Items[i].Age;

Create a new object of class Item and initialize it's member variables using list item. Also for this either set member variables Name and Age as public or expose them through public properties.

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20