3

class example:

public class Customer
{
    public int CustomerID { get; set; }
}

using the class:

    Customer customer1 = new Customer();
    customer1.CustomerID = 1;

Now how can I create a customer2 class with all the values that are stored in customer1?

JC Lizard
  • 1,050
  • 6
  • 17
  • no inheritence. I initialize a class and I start feeding alot of information to it. now how to clone it with all the info that I have fed into it? – JC Lizard Dec 19 '13 at 19:39
  • 1
    Take a look at: http://stackoverflow.com/questions/78536/deep-cloning-objects-in-c-sharp – Swati Dec 19 '13 at 19:40

4 Answers4

6

You can do it manually:

var customer2 = new Customer { CustomerID = customer1.CustomerID };

You can implement ICloneable interface in Customer class:

public class Customer : ICloneable
{
    private int CustomerID { get; set; }

    public Customer Clone()
    {
        return new Customer { CustomerID = this.CustomerID };
    }

    object ICloneable.Clone()
    {
        return this.Clone();
    }
}

and then use it:

var customer2 = customer1.Clone();

You can serialize your object into XML/JSON and then deserialize it into new object, as described in this answer: Deep cloning objects in C#.

Or you can use reflection to get and copy all properties/fields values into your new Customer instance. It could have bad performance, but you'd have to measure it to make sure how bad it is.

Edit

One more way to do that: you can make reflection version faster using Expression Tree! Get all fields/properties and compile all necessary assignments at runtime using Expression.Lambda. After that every next Clone call will use compiled code so there will be no performance drawback at all. I've created Clone<T> extension method which does exactly that, using Expression class, static constructor and reflection. You can find the code on CodePlex: CloneExtension.cs

Community
  • 1
  • 1
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
1

Either you use reflection to copy the values or you would want deep clone (ICloneable).

123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
1

To extend Marcin's answer, if all of your items in your class are value types or immutable types (int, double, string, ect.) you can just use MemberwiseClone(). This will create a shallow copy of the original object, and if all your members are immutable there is no need to do a deep copy. This can be useful if you have many objects in your class you need to copy over.

public sealed class Customer : ICloneable
{
    private int CustomerID { get; set; }

    public Customer Clone()
    {
        return (customer)this.MemberwiseClone();
    }

    object ICloneable.Clone()
    {
        return this.Clone();
    }
}
Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • If all members are immutable, why create a clone? :) – Thorarin Dec 19 '13 at 20:50
  • @Thorarin Good question, because the members are immutable but the container class that stores them (`Customer`) is not. If I do `myCustomer2 = myCustmer1.Clone(); myCustomer2.CustomerID = 3` I do not want `myCustomer1` to be modified with the new value of `CustomerID`. – Scott Chamberlain Dec 19 '13 at 20:52
0

What if you set the class up like so:

public class Customer
{
  private Customer Parent {get; private set;}

  private int _id;
  public int CustomerID
  {
    get { return Parent == null ? _id : Parent.CustomerID; }
    set
    {
      if(Parent != null)
        throw new InvalidOperationException("...");

      _id = value;
    }

    public Customer()
    {
    }

    public static Customer Clone(Customer parent)
    {
      return new Customer{Parent = parent};
    }
}

This would create a immutable clone. Now... if you need to be able to alter the values... then either take a different approach, or expand on it.

poy
  • 10,063
  • 9
  • 49
  • 74