0

I would like to know if is there any way to copy one object to the other, without changing their memory locations, in example below.

 class PersonData
    {
        public string PersonName;
        public int age;

        public PersonData(string name, int age)
        {
            this.age = age;
            PersonName = name;
        }
    }
    class Program
    {

        static void Main(string[] args)
        {
            object person1 = new PersonData("FirstPerson",20);
            object person2 = new PersonData("secondPerson",30);

            person1 = person2;
        }
    }

person1 will start pointing to the memory location of person2. what I would like to do just copy the VALUES of person2 at the memory location of person1. is there any method other than

person1.age = person2.age;
person1.name = person2.name;

because I don't know the fields of the object beforehand.

thank you in advance.

Kevin
  • 7,162
  • 11
  • 46
  • 70
  • 1
    You're talking about cloning objects. [Deep cloning objects](https://stackoverflow.com/questions/78536/deep-cloning-objects) – Patrick Tucci Apr 28 '20 at 16:05
  • @ patrick Tucci yes but cloning in a way it should not change the memory location of person1. for instance, person1 pointing 123 memory block and person2 pointing 456. I want to copy the value of person2 at 123 memory location. – muhayyuddin gillani Apr 30 '20 at 12:28
  • What do you mean you want to `copy the value of person2 at 123 memory location`? That doesn't make sense. For reference types, like your `PersonData` objects, variables hold references to objects. Do you want to copy the reference from `person2` to `person1`, so they both refer to the same object? Or do you want to copy the values from the object referenced by `person2` to the object referenced by `person1`? – Patrick Tucci May 01 '20 at 12:33

2 Answers2

1

All objects have a protected function called MemberwiseClone that does a shallow copy of the members of a class. Typically this is exposed via implementing the ICloneable interface. However, ensure you understand what shallow copy means. For value-type member variables it copies the values. For reference-type member variables it copies the reference.

public class PersonData : ICloneable {
    public string PersonName;
    public int age;

    public PersonData(string name, int age)
    {
        this.age = age;
        PersonName = name;
    }

    public object Clone() => this.MemberwiseClone();
}

class Program
{

    static void Main(string[] args)
    {
        object person1 = new PersonData("FirstPerson",20);
        object person2 = new PersonData("secondPerson",30);

        person1 = person2.Clone();
    }
}
Aaron G
  • 63
  • 6
  • for a reference type, it will change the memory location of person1. i want to copy the value at the memory location of person1 – muhayyuddin gillani Apr 30 '20 at 12:32
  • Reflection is the only way I can think of to do what you are asking though I’d have to say this probably isn’t a good idea. var fields = typeof(Person).GetFields(BindingFlags.Instance); – Aaron G May 01 '20 at 14:13
  • Sorry, trying to type on my phone. var fields = typeof(Person).GetFields(BindingFlags.Instance); foreach(var field in fields) { var value = field.GetValue(person2); field.SetValue(person1, value);} – Aaron G May 01 '20 at 14:23
1

Use deep copy,

Deep copy will create a instance and it copy value to own memory location.

.Net have a many way to do this.

I think use serialize object is the most easy way.

Sample code (Use Newtonsoft.Json package):

 class PersonData
    {
        public string PersonName;
        public int age;

        public PersonData(string name, int age)
        {
            this.age = age;
            PersonName = name;
        }
    }
    class Program
    {

        static void Main(string[] args)
        {
            object person1 = new PersonData("FirstPerson",20);
            object person2 = new PersonData("secondPerson",30);

            //First ,serialize the object, then copy to other object with deserialize
            person2 = JsonConvert.DeserializeObject<Person>(JsonConvert.SerializeObject(source));
        }
    }
Vic
  • 758
  • 2
  • 15
  • 31