0

Ok, I know that Copy Ctor copies the object's data to another object. But I'm still not clear what the practical use of Copy Ctor in real time development is. I have not found any satisfying answers, just definitions of what it is.

Ben
  • 2,433
  • 5
  • 39
  • 69
neeraj
  • 57
  • 1
  • 5
  • 9
  • 1
    you don't write copy constructors in C#. look at the ICloneable.Clone Method – ro-E May 06 '15 at 02:01
  • 1
    C# doesn't have copy constructors the way C++ does, so I'm not sure what you are expecting as an answer. You can write something that looks like a copy constructor but there's nothing special about it. – Mike Zboray May 06 '15 at 02:03
  • 2
    @ro-E You *can* and arguably should, write copy constructors in C#. IClonable is not really a very good solution; see top answer at robert's linked question. – Blorgbeard May 06 '15 at 02:13
  • The practical use is making a copy of an object. I'm not sure what more than that you want to know.. – Blorgbeard May 06 '15 at 02:23

1 Answers1

0

c# does not have copy constructor. System.Object class has MemberwiseClone method which creates shallow copy for reference types.

In shallow copy the value types fields are copied as it is. For reference type fields, the address is copied in the newly created object.

Deep copy of an object is created when actual objects are allocated for reference field types.

An example of deep copy can be found at: https://msdn.microsoft.com/en-us/library/ms173116.aspx

Milli
  • 74
  • 4