public static void Main() { Person p1 = new Person(); p1.age = 42; p1.birthDate = Convert.ToDateTime("1977-01-01"); p1.name = "Jack"; p1.idInfo = new IdInfo(1414); // Perform a shallow copy of p1 and assign it to p2 Person p2 = p1.ShallowCopy(); // Make a deep copy of p1 and assign it to p3 Person p3 = p1.ShallowCopy(); // Display values of p1, p2 and p3. Console.WriteLine("Original values of p1, p2, p3:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values: "); DisplayValues(p2); Console.WriteLine(" p3 instance values: "); DisplayValues(p3); // Change the value of p1 properties and display the values of p1, p2 and p3 p1.age = 32; p1.birthDate = Convert.ToDateTime("1900-01-01"); p1.name = "Frank"; p1.idInfo.idNumber = 7878; Console.WriteLine("\nValues of p1, p2, and p3 after changes to p1: "); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values (reference values have changed): "); DisplayValues(p2); Console.WriteLine(" p3 instance values (everything was kept the same): "); DisplayValues(p3);
static void Main(string[] args) { Person p1 = new Person(); p1.Age = 24; p1.BirthDate = Convert.ToDateTime("1995-01-20"); p1.Name = "Jovan Petkoski"; p1.Id = new IdInfo(23); // Perform a shallow copy of p1 and assign it to p2. Person p2 = p1.ShallowCopy(); // Make a deep copy of p1 and assign it to p3. Person p3 = p1.DeepCopy(); // Display values of p1, p2 and p3. Console.WriteLine("Original values of p1, p2, p3:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); Console.WriteLine(" p3 instance values:"); DisplayValues(p3); // Change the value of p1 properties and display the values of p1, p2 and p3. p1.Age = 34; p1.BirthDate = Convert.ToDateTime("2005-02-22"); p1.Name = "Frank"; p1.Id.IdNumber = 33; Console.WriteLine("\nValues of p1, p2 and p3 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values (reference values have changed):"); DisplayValues(p2); Console.WriteLine(" p3 instance values (everything was kept the same):"); DisplayValues(p3); // Shallow copy issue: Console.WriteLine(); Console.WriteLine(); var john = new Person { Id = new IdInfo(1), Name = "John Smith", Age = 18, BirthDate = new DateTime(2000, 05, 25) }; var jane = john.ShallowCopy(); jane.Name = "Jane Smith"; //John's name DID NOT change (good) jane.Age = 28; //Same here (good) jane.BirthDate = new DateTime(2005, 06, 26); //Same here (good) jane.Id.IdNumber = 2; //John's Id changed (bad) DisplayValues(john); DisplayValues(jane); Console.Read(); }
static void Main(string[] args) { Person person1 = new Person(); person1.Age = 18; person1.Gender = 'M'; person1.Fullname = new FullName { FirstName = "Jie1", LastName = "Chen" }; Console.WriteLine(person1.ToString()); Console.WriteLine(); Person person2 = person1.ShallowCopy(); person1.Age = 28; person1.Fullname = new FullName { FirstName = "Jie11", LastName = "Chen" }; Console.WriteLine(person1.ToString()); Console.WriteLine(person2.ToString()); Console.WriteLine(); Person person3 = person1.DeepCopy(); person1.Age = 38; person1.Fullname = new FullName { FirstName = "Jie111", LastName = "Chen" }; Console.WriteLine(person1.ToString()); Console.WriteLine(person2.ToString()); Console.WriteLine(person3.ToString()); }
static void Main(string[] args) { var p1 = new Person(); p1.Name = "p1"; p1.Prototype = new Prototype(1); // shallow copy var p2 = p1.ShallowCopy(); // deep copy var p3 = p1.DeepCopy(); System.Console.WriteLine($"name p1 p2 p3"); System.Console.WriteLine($" {p1.Name} {p2.Name} {p3.Name}"); System.Console.WriteLine($"protoId p1 p2 p3"); System.Console.WriteLine($" {p1.Prototype.Id} {p2.Prototype.Id} {p3.Prototype.Id}"); // change p1 System.Console.WriteLine("change p1 name to p5"); p1.Name = "p5"; System.Console.WriteLine($"name p1 p2 p3"); System.Console.WriteLine($" {p1.Name} {p2.Name} {p3.Name}"); System.Console.WriteLine($"protoId p1 p2 p3"); System.Console.WriteLine($" {p1.Prototype.Id} {p2.Prototype.Id} {p3.Prototype.Id}"); // change p1.Prototype.Id System.Console.WriteLine("change p1.Prototype.Id to 5"); p1.Prototype.Id = 5; System.Console.WriteLine($"name p1 p2 p3"); System.Console.WriteLine($" {p1.Name} {p2.Name} {p3.Name}"); System.Console.WriteLine($"protoId p1 p2 p3"); System.Console.WriteLine($" {p1.Prototype.Id} {p2.Prototype.Id} {p3.Prototype.Id}"); }
static void Main(string[] args) { Person p1 = new Person(); p1.Age = 42; p1.BirthDate = Convert.ToDateTime("1977-01-01"); p1.Name = "Jack Daniels"; p1.IdInfo = new IdInfo(666); Person p2 = p1.ShallowCopy(); Person p3 = p1.DeepCopy(); Console.WriteLine("Original values of p1, p2, p3:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); Console.WriteLine(" p3 instance values:"); DisplayValues(p3); p1.Age = 32; p1.BirthDate = Convert.ToDateTime("1900-01-01"); p1.Name = "Frank"; p1.IdInfo.IdNumber = 7878; Console.WriteLine("\nValues of p1, p2 and p3 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values (reference values have changed):"); DisplayValues(p2); Console.WriteLine(" p3 instance values (everything was kept the same):"); DisplayValues(p3); }
private static void TestShallowCopy() { Person p1 = new Person(); p1.Age = 42; p1.Name = "Jack Daniels"; p1.IdInfo = new IdInfo(666); Person p2 = p1.ShallowCopy(); Console.Clear(); Console.WriteLine("TestShallowCopy"); Console.WriteLine("Original values of p1, p2"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); p1.Age = 32; p1.Name = "Frank"; p1.IdInfo.IdNumber = 7878; Console.WriteLine("\nValues of p1 and p2 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); Wait(); }
private static void Main() { var p1 = new Person { Age = 42, BirthDate = Convert.ToDateTime("1977-01-01"), Name = "Jack Daniels", IdInfo = new IdInfo(666) }; // Выполнить поверхностное копирование p1 и присвоить её p2. var p2 = p1.ShallowCopy(); // Сделать глубокую копию p1 и присвоить её p3. var p3 = p1.DeepCopy(); // Сделать копию через метод Clone интерфейса IClonable var p4 = (Person)p1.Clone(); // Вывести значения p1, p2 и p3 и p4. Console.WriteLine("Original values of p1, p2, p3:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); Console.WriteLine(" p3 instance values:"); DisplayValues(p3); Console.WriteLine(" p4 instance values:"); DisplayValues(p4); // Изменить значение свойств p1 и отобразить значения p1, p2 и p3 и p4. p1.Age = 32; p1.BirthDate = Convert.ToDateTime("1900-01-01"); p1.Name = "Frank"; p1.IdInfo.IdNumber = 7878; Console.WriteLine("\nValues of p1, p2 and p3 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values (reference values have changed):"); DisplayValues(p2); Console.WriteLine(" p3 instance values (everything was kept the same):"); DisplayValues(p3); Console.WriteLine(" p4 instance values (everything was kept the same):"); DisplayValues(p4); }
static void Main(string[] args) { var obj = new Person() { Age = 19, BirthDate = Convert.ToDateTime("1998-02-01"), Name = "Denchickobraz", IdInfo = new IdInfo(1) }; var newObj1 = obj.ShallowCopy(); var newObj2 = obj.DeepCopy(); obj.IdInfo.IdNumber = 22; Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Prototype design pattern example"); Console.WriteLine("Prototype is a creational design pattern that allows cloning objects, even complex ones, without coupling to their specific classes."); Console.WriteLine(); Person person1 = new Person { Age = 43, BirthDate = Convert.ToDateTime("1976-01-01"), Name = "Stuart Shepherd", IdInfo = new IdInfo(666) }; // perform a shallow copy of person1 and assign it to person2. Person person2 = person1.ShallowCopy(); // make a deep copy of person1 and assign it to person3. Person person3 = person1.DeepCopy(); // display values of person1, person2 and p3. Console.WriteLine("Original values of person1, person2, person3:"); Console.WriteLine(" person1 instance values: "); DisplayValues(person1); Console.WriteLine(" person2 instance values:"); DisplayValues(person2); Console.WriteLine(" person3 instance values:"); DisplayValues(person3); // change the value of person1 properties and display the values of person1, person2 and person3. person1.Age = 20; person1.BirthDate = Convert.ToDateTime("2000-01-01"); person1.Name = "John"; person1.IdInfo.IdNumber = 7878; Console.WriteLine("\nValues of person1, person2 and person3 after changes to person1:"); Console.WriteLine(" person1 instance values: "); DisplayValues(person1); Console.WriteLine(" person2 instance values (reference values have changed):"); DisplayValues(person2); Console.WriteLine(" person3 instance values (everything was kept the same):"); DisplayValues(person3); }
static void Main(string[] args) { Person p1 = new Person(); p1.Age = 42; p1.BirthDate = Convert.ToDateTime("1977-01-01"); p1.Name = "Jack"; p1.SecondName = "Daniels"; p1.IdInfo = new IdInfo(666); p1.Address = new Address("Kings Road", 15); Person p2 = p1.ShallowCopy(); Person p3 = (Person)p1.Clone(); Console.WriteLine("Original values of p1, p2, p3:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); Console.WriteLine(" p3 instance values:"); DisplayValues(p3); p1.Age = 32; p1.BirthDate = Convert.ToDateTime("1900-01-01"); p1.Name = "Frank"; p1.SecondName = "Simpson"; p1.IdInfo.IdNumber = 7878; p1.Address = new Address("Wall street", 10); Console.WriteLine("\nValues of p1, p2 and p3 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values (reference values have changed):"); DisplayValues(p2); Console.WriteLine(" p3 instance values (everything was kept the same):"); DisplayValues(p3); Console.ReadLine(); }
static void Main(string[] args) { //Create an instance of person and assign values to it fields Person p1 = new Person(); p1.Age = 22; p1.IdInfo = new IdInfo(3242); //perfoem a shallow copy of p1 and assign it to p2 Person p2 = p1.ShallowCopy(); //Display Values of p1 & p2 Console.WriteLine("Original values of p1 & p2"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values: "); DisplayValues(p2); p1.Age = 25; p1.IdInfo = new IdInfo(1121); //Display Values of p1 & p2 Console.WriteLine("\nValues of p1 & p2 after changed to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values: "); DisplayValues(p2); Person p3 = p1.DeepCopy(); p1.Age = 28; p1.IdInfo = new IdInfo(1234); //Display Values of p1 & p2 Console.WriteLine("values of p1 & p3 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values: "); DisplayValues(p3); Console.ReadKey(); }
static void Main(string[] args) { //create inital object Person p1 = new Person(); p1.Age = 42; p1.BirthDate = Convert.ToDateTime("1977-01-01"); p1.Name = "Jack Daniels"; p1.IdInfo = new IdInfo(666); // Perform a shallow copy of p1 and assign it to p2. Person shallowCoppyOfP1 = p1.ShallowCopy(); // Make a deep copy of p1 and assign it to p3. Person deepCopyOfP1 = p1.DeepCopy(); // Display values of p1, p2 and p3. Console.WriteLine("Original values of p1, p2, p3:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" shallow copy of p1 instance values:"); DisplayValues(shallowCoppyOfP1); Console.WriteLine(" deep copy of p1 instance values:"); DisplayValues(deepCopyOfP1); // Change the value of p1 properties and display the values of p1, // p2 and p3. p1.Age = 32; p1.BirthDate = Convert.ToDateTime("1900-01-01"); p1.Name = "Frank"; p1.IdInfo.IdNumber = 7878; Console.WriteLine("\nValues of p1, p2 and p3 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" shallow copy of p1 instance values (reference values have changed):"); DisplayValues(shallowCoppyOfP1); Console.WriteLine(" deep copy of p1 instance values (everything was kept the same):"); DisplayValues(deepCopyOfP1); }
static void Main(string[] args) { var p1 = new Person("Andrey", 17, new Dog("Tuzik", 3)); p1.Print(); var p2 = p1.ShallowCopy(); var p3 = p1.DeepCopy(); p1.Dog.Name = "123"; Console.WriteLine("Shallow copy doesn't properly copy inner reference types (except String)!"); p2.Print(); Console.WriteLine("Deep copy properly copy inner reference types."); p3.Print(); Console.WriteLine(); Console.WriteLine("Prototype registry usage"); Console.WriteLine(); var registry = new PersonRegistry(); registry.Add(1, new Person("Alex", 30, new Dog("Ralf", 5))); registry.Add(2, new Person("Dasha", 20, new Dog("Barni", 4))); registry.Add(3, new Person("Egor", 15, new Dog("Bobik", 8))); var p4 = registry.GetById(1); var p5 = registry.GetById(1); p5.Print(); p4.Dog.Name = "something else"; Console.WriteLine("Clones from registry are not connected to each other."); p5.Print(); }
static void Main(string[] args) { Person p1 = new Person(); p1.Age = 42; p1.BirthDate = Convert.ToDateTime("1977-01-01"); p1.Name = "Jack Daniels"; p1.IdInfo = new IdInfo(666); // chentoz : this is a "reference // perform a shallow copy of p1 and assign it to p2 Person p2 = p1.ShallowCopy(); // make a deep copy of p1 and assign it to p3 Person p3 = p1.DeepCopy(); // Display values of p1, p2 and p3. Console.WriteLine("Original values of p1, p2, p3:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values:"); DisplayValues(p2); Console.WriteLine(" p3 instance values:"); DisplayValues(p3); // Change values p1.Age = 32; p1.BirthDate = Convert.ToDateTime("1900-01-01"); p1.Name = "Frank"; p1.IdInfo.IdNumber = 7878; // <------------------------------ shallow copy Console.WriteLine("\nValues of p1, p2 and p3 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(p1); Console.WriteLine(" p2 instance values (reference values have changed):"); DisplayValues(p2); Console.WriteLine(" p3 instance values (everything was kept the same):"); DisplayValues(p3); }
static void Main(string[] args) { //Console.WriteLine("Hello World!"); var mainPerson = new Person { Age = 12, BirthDate = DateTime.Now, IdInfo = new IdInfo(10), Name = "Pepi Tsesmeli" }; //Console.WriteLine("Main Person " + mainPerson.Age ); //Console.WriteLine("Main Person " + mainPerson.BirthDate); //Console.WriteLine("Main Person " + mainPerson.Name); //Console.WriteLine("Main Person " + mainPerson.IdInfo.IdNumber); var shallowCopy = mainPerson.ShallowCopy(); //Console.WriteLine("Shallow Copy " + shallowCopy.Age); //Console.WriteLine("Shallow Copy " + shallowCopy.BirthDate); //Console.WriteLine("Shallow Copy " + shallowCopy.Name); //Console.WriteLine("Shallow Copy " + shallowCopy.IdInfo.IdNumber); var deepCopy = mainPerson.DeepCopy(); //Console.WriteLine("Shallow Copy " + deepCopy.Age); //Console.WriteLine("Shallow Copy " + deepCopy.BirthDate); //Console.WriteLine("Shallow Copy " + deepCopy.Name); //Console.WriteLine("Shallow Copy " + deepCopy.IdInfo.IdNumber); mainPerson.Age = 32; mainPerson.BirthDate = Convert.ToDateTime("1900-01-01"); mainPerson.Name = "Frank"; mainPerson.IdInfo.IdNumber = 7878; Console.WriteLine("\nValues of p1, p2 and p3 after changes to p1:"); Console.WriteLine(" p1 instance values: "); DisplayValues(mainPerson); Console.WriteLine(" p2 instance values (reference values have changed):"); DisplayValues(shallowCopy); Console.WriteLine(" p3 instance values (everything was kept the same):"); DisplayValues(deepCopy); }