static void Main(string[] args) { var person1 = new Person(new[] { "darren", "ji" }, new Address("qingdao", 100)); var person2 = person1.DeepCopy(); person2.Names[0] = "sunny"; person2.Address.StreetName = "jinan"; person2.Address.StreetNo = 999; Console.WriteLine(person1.ToString()); Console.WriteLine(person2.ToString()); }
static void Main(string[] args) { var john = new Person(new[] { "John", "Smith" }, new Address("London Road", 123)); var jane = john.DeepCopy(); jane.Names[0] = "Jane"; jane.Address.HouseNumber = 321; Console.WriteLine(john); Console.WriteLine(jane); }
static void Main(string[] args) { var john = new Person(new [] { "John", "Smith" }, new Address("Hobart", 123)); //using the clone this way dosent accomplish anything because the change //for Janes house number changes johns as well var jane = john.DeepCopy(); jane.Names[0] = "Jane"; jane.Address.HouseNumber = 321; WriteLine(john); WriteLine(jane); }
static void Main(string[] args) { Console.WriteLine("--- Deep Copy ---"); Console.WriteLine("--- First Person ---"); Person firstPerson = new Person("Çağan Topçu", 28, 185, 85, GenderEnum.Male, new Address(new City("İzmir"), new Country("Türkiye"))); Console.WriteLine("1. Person's Name : " + firstPerson.Name); Console.WriteLine("2. Person's City: " + firstPerson.Address.City.Name); Console.WriteLine("1. Person's Age : " + firstPerson.Age); Console.WriteLine("1. Person's Height : " + firstPerson.Height); Console.WriteLine("1. Person's Weight : " + firstPerson.Weight); Console.WriteLine("----- Second Person ---"); Person secondPerson = firstPerson.DeepCopy(); secondPerson.Name = "Michael Jordan"; secondPerson.Address.City.Name = "Florida"; Console.WriteLine("2. Person's Name : " + secondPerson.Name); Console.WriteLine("2. Person's City: " + secondPerson.Address.City.Name); Console.WriteLine("1. Person's Age : " + secondPerson.Age); Console.WriteLine("2. Person's Height : " + secondPerson.Height); Console.WriteLine("2. Person's Weight : " + secondPerson.Weight); //Console.WriteLine("--- Shallow Copy ---"); // Console.WriteLine("--- First Person ---"); // Person firstPerson = new Person("Çağan Topçu", 28, 185, 85, GenderEnum.Male, new Address(new City("İzmir"), new Country("Türkiye"))); //Console.WriteLine("1. Person's Name : " + firstPerson.Name); // Console.WriteLine("2. Person's City: " + firstPerson.Address.City.Name); // Console.WriteLine("1. Person's Age : " + firstPerson.Age); // Console.WriteLine("1. Person's Height : " + firstPerson.Height); // Console.WriteLine("1. Person's Weight : " + firstPerson.Weight); // Console.WriteLine("----- Second Person ---"); // Person secondPerson = firstPerson.ShallowCopy(); //secondPerson.Name = "Michael Jordan"; // secondPerson.Address.City.Name = "Florida"; // Console.WriteLine("2. Person's Name : " + secondPerson.Name); // Console.WriteLine("2. Person's City: " + secondPerson.Address.City.Name); // Console.WriteLine("1. Person's Age : " + secondPerson.Age); // Console.WriteLine("2. Person's Height : " + secondPerson.Height); // Console.WriteLine("2. Person's Weight : " + secondPerson.Weight); }
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(); }
private static void Test2() { var person = new Person() { Address = new Address() { StreetName = "Mane Vasti", City = "Pune" }, Name = "Kuldeep Singh" }; var newPerson = person.DeepCopy(); newPerson.Name = "Ravneet Kaur"; newPerson.Address.StreetName = "Lane No 3,Mane Vasti"; Console.WriteLine(person); Console.WriteLine(newPerson); }
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) { //For Copy Constructors var john = new Person(new[] { "John", "Smith" }, new Address("London Road", 123)); var jane = new Person(john); jane.Address.HouseNumber = 321; jane.Names = new[] { "Jane", "Silva" }; WriteLine(john); WriteLine(jane); WriteLine(); //Copy Interface var john2 = new Person(new[] { "John", "Smith" }, new Address("London Road", 123)); var jane2 = john.DeepCopy(); jane.Address.HouseNumber = 321; jane.Names = new[] { "Jane", "Silva" }; WriteLine(john2); WriteLine(jane2); 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); }