Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the method MemberwiseClone() doing?

I am confused with this code below,

Developer devCopy = (Developer)dev.Clone(); 

Clone method of Developer class just creating a Employee clone, then how developer get another clone of developer.

public abstract class Employee {     public abstract Employee Clone();      public string Name { get; set; }     public string Role { get; set; } }   public class Typist : Employee {     public int WordsPerMinute { get; set; }      public override Employee Clone()     {         return (Employee)MemberwiseClone();     }      public override string ToString()     {         return string.Format("{0} - {1} - {2}wpm", Name, Role, WordsPerMinute);     } }   public class Developer : Employee {     public string PreferredLanguage { get; set; }      public override Employee Clone()     {         return (Employee)MemberwiseClone();     }      public override string ToString()     {         return string.Format("{0} - {1} - {2}", Name, Role, PreferredLanguage);     } }   Developer dev = new Developer(); dev.Name = "Bob"; dev.Role = "Team Leader"; dev.PreferredLanguage = "C#";  Developer devCopy = (Developer)dev.Clone(); devCopy.Name = "Sue";  Console.WriteLine(dev); Console.WriteLine(devCopy);  /* OUTPUT  Bob - Team Leader - C# Sue - Team Leader - C#  */  Typist typist = new Typist(); typist.Name = "Kay"; typist.Role = "Typist"; typist.WordsPerMinute = 120;  Typist typistCopy = (Typist)typist.Clone(); typistCopy.Name = "Tim"; typistCopy.WordsPerMinute = 115;  Console.WriteLine(typist); Console.WriteLine(typistCopy);  /* OUTPUT  Kay - Typist - 120wpm Tim - Typist - 115wpm  */ 
like image 968
Syed Tayyab Ali Avatar asked Feb 18 '10 14:02

Syed Tayyab Ali


People also ask

How do I copy one object to another in C#?

In general, when we try to copy one object to another object, both the objects will share the same memory address. Normally, we use assignment operator, = , to copy the reference, not the object except when there is value type field. This operator will always copy the reference, not the actual object.

What is shallow copy and deep copy in C#?

This is called “Shallow Copy”. To get the same behavior for a Reference Type as well as a Value Type we use the Clone() method that belongs to the System. ICloneable interface. This is called a “Deep Copy”. We will see both behaviors in depth one by one.


1 Answers

Because the method MemberwiseClone() is doing this for you. See the documentation

The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.

Whenever you see a method you don't unerstand, you can trace who has declared it (in Visual Studio, I guess), and in turn see its documentation. That makes things pretty obvious most of the time.

like image 132
Bozho Avatar answered Sep 28 '22 09:09

Bozho