Mohammad Shakermohammadshaker.com
C# Programming Course@ZGTRShaker
2011, 2012, 2013, 2014
C# StarterL07 – Objects Cloning
uses Object Cloning to save the whole engine stateThe user can then switch back to any point in time!
See more of @ http://mohammadshakergtr.wordpress.com/
and @ http://www.youtube.com/watch?v=FM3v0tbdKrs
Cloning Objects – The Concept
Shallow Clone VS Deep Clone
Cloning Objects
using System;using System.Reflection;
namespace ConsoleApplicationCourseTest{
class Mobile{
public string Number { set; get; }}
class MainClass{
public static void Main(String[] args){
Mobile M1 = new Mobile(), M2 = new Mobile();M1.Number = "0000";M2.Number = "0001";M2 = M1;Console.WriteLine("M1 Num. {0} \nM2 Num. {1}", M1.Number, M2.Number);
}}
}
Cloning Objects
using System;using System.Reflection;
namespace ConsoleApplicationCourseTest{
class Mobile{
public string Number { set; get; }}
class MainClass{
public static void Main(String[] args){
Mobile M1 = new Mobile(), M2 = new Mobile();M1.Number = "0000";M2.Number = "0001";M2 = M1;Console.WriteLine("M1 Num. {0} \nM2 Num. {1}", M1.Number, M2.Number);
}}
}
M1 Num. 0000M2 Num. 0000Press any key to continue...
Cloning Objects
using System;using System.Reflection;
namespace ConsoleApplicationCourseTest{
class Mobile{
public string Number { set; get; }}
class MainClass{
public static void Main(String[] args){
Mobile M1 = new Mobile(), M2 = new Mobile();M1.Number = "0000";M2.Number = "0001";M2 = M1;Console.WriteLine("M1 Num. {0} \nM2 Num. {1}", M1.Number, M2.Number);
}}
}
M1 Num. 0000M2 Num. 0000Press any key to continue...
Cloning Objects
using System;using System.Reflection;
namespace ConsoleApplicationCourseTest{
class Mobile{
public string Number { set; get; }}
class MainClass{
public static void Main(String[] args){
Mobile M1 = new Mobile(), M2 = new Mobile();M1.Number = "0000";M2.Number = "0001";M2 = M1;Console.WriteLine("M1 Num. {0} \nM2 Num. {1}", M1.Number, M2.Number);
}}
}
M1 Num. 0000M2 Num. 0000Press any key to continue...
Cloning Objects
using System;using System.Reflection;
namespace ConsoleApplicationCourseTest{
class Mobile{
public string Number { set; get; }}
class MainClass{
public static void Main(String[] args){
Mobile M1 = new Mobile(), M2 = new Mobile();M1.Number = "0000";M2.Number = "0001";M2 = M1;Console.WriteLine("M1 Num. {0} \nM2 Num. {1}", M1.Number, M2.Number);
}}
}
M1 Num. 0000M2 Num. 0000Press any key to continue...
Cloning Objects
using System;using System.Reflection;
namespace ConsoleApplicationCourseTest{
class Mobile{
public string Number { set; get; }}
class MainClass{
public static void Main(String[] args){
Mobile M1 = new Mobile(), M2 = new Mobile();M1.Number = "0000";M2.Number = "0001";M2 = M1;Console.WriteLine("M1 Num. {0} \nM2 Num. {1}", M1.Number, M2.Number);
}}
}
M1 Num. 0000M2 Num. 0000Press any key to continue...
Cloning Objects
using System;using System.Reflection;
namespace ConsoleApplicationCourseTest{
class Mobile{
public string Number { set; get; }}
class MainClass{
public static void Main(String[] args){
Mobile M1 = new Mobile(), M2 = new Mobile();M1.Number = "0000";M2.Number = "0001";M2 = M1;Console.WriteLine("M1 Num. {0} \nM2 Num. {1}", M1.Number, M2.Number);
}}
}
M1 Num. 0000M2 Num. 0000Press any key to continue...
Cloning Objects
using System;using System.Reflection;
namespace ConsoleApplicationCourseTest{
class Mobile{
public string Number { set; get; }}
class MainClass{
public static void Main(String[] args){
Mobile M1 = new Mobile(), M2 = new Mobile();M1.Number = "0000";M2.Number = M1.Number;Console.WriteLine("M1 Num. {0} \nM2 Num. {1}", M1.Number, M2.Number);
}}
}
M1 Num. 0000M2 Num. 0000Press any key to continue...
Cloning Objects
using System;using System.Reflection;
namespace ConsoleApplicationCourseTest{
class Mobile{
public string Number { set; get; }}
class MainClass{
public static void Main(String[] args){
Mobile M1 = new Mobile(), M2 = new Mobile();M1.Number = "0000";M2.Number = M1.Number;Console.WriteLine("M1 Num. {0} \nM2 Num. {1}", M1.Number, M2.Number);
}}
}
M1 Num. 0000M2 Num. 0000Press any key to continue...
Cloning Objects
using System;using System.Reflection;
namespace ConsoleApplicationCourseTest{
class Mobile{
public string Number { set; get; }}
class MainClass{
public static void Main(String[] args){
Mobile M1 = new Mobile(), M2 = new Mobile();M1.Number = "0000";M2.Number = M1.Number;Console.WriteLine("M1 Num. {0} \nM2 Num. {1}", M1.Number, M2.Number);
}}
}
M1 Num. 0000M2 Num. 0000Press any key to continue...
Cloning Objects
using System;using System.Reflection;
namespace ConsoleApplicationCourseTest{
class Mobile{
public string Number { set; get; }}
class MainClass{
public static void Main(String[] args){
Mobile M1 = new Mobile(), M2 = new Mobile();M1.Number = "0000";M2.Number = M1.Number;Console.WriteLine("M1 Num. {0} \nM2 Num. {1}", M1.Number, M2.Number);
}}
}
M1 Num. 0000M2 Num. 0000Press any key to continue...
Try to Clone with IClonable interface
Cloning Objects
• ICloneable interface
• Another disadvantage of ICloneable is that the Clone method returns an object,
hence every Clone call requires a cast:
public interface ICloneable{
object Clone();}
Person joe = new Person();joe.Name = "Joe Smith";Person joeClone = (Person)joe.Clone();
namespace ConsoleApplicationCourseTest{
class MainClass{
public class Person : ICloneable{
public string Name;
object ICloneable.Clone(){
return this.Clone();}
public Person Clone(){
return (Person)this.MemberwiseClone();}
}public static void Main(String[] args){
Person joe = new Person();joe.Name = "Joe Smith";
Person bob = (Person)joe.Clone();Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
bob.Name = "Bob PopUp";Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
}}
}
namespace ConsoleApplicationCourseTest{
class MainClass{
public class Person : ICloneable{
public string Name;
object ICloneable.Clone(){
return this.Clone();}
public Person Clone(){
return (Person)this.MemberwiseClone();}
}public static void Main(String[] args){
Person joe = new Person();joe.Name = "Joe Smith";
Person bob = (Person)joe.Clone();Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
bob.Name = "Bob PopUp";Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
}}
}
namespace ConsoleApplicationCourseTest{
class MainClass{
public class Person : ICloneable{
public string Name;
object ICloneable.Clone(){
return this.Clone();}
public Person Clone(){
return (Person)this.MemberwiseClone();}
}public static void Main(String[] args){
Person joe = new Person();joe.Name = "Joe Smith";
Person bob = (Person)joe.Clone();Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
bob.Name = "Bob PopUp";Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
}}
}
namespace ConsoleApplicationCourseTest{
class MainClass{
public class Person : ICloneable{
public string Name;
object ICloneable.Clone(){
return this.Clone();}
public Person Clone(){
return (Person)this.MemberwiseClone();}
}public static void Main(String[] args){
Person joe = new Person();joe.Name = "Joe Smith";
Person bob = (Person)joe.Clone();Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
bob.Name = "Bob PopUp";Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
}}
}
namespace ConsoleApplicationCourseTest{
class MainClass{
public class Person : ICloneable{
public string Name;
object ICloneable.Clone(){
return this.Clone();}
public Person Clone(){
return (Person)this.MemberwiseClone();}
}public static void Main(String[] args){
Person joe = new Person();joe.Name = "Joe Smith";
Person bob = (Person)joe.Clone();Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
bob.Name = "Bob PopUp";Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
}}
}
namespace ConsoleApplicationCourseTest{
class MainClass{
public class Person : ICloneable{
public string Name;
object ICloneable.Clone(){
return this.Clone();}
public Person Clone(){
return (Person)this.MemberwiseClone();}
}public static void Main(String[] args){
Person joe = new Person();joe.Name = "Joe Smith";
Person bob = (Person)joe.Clone();Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
bob.Name = "Bob PopUp";Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
}}
}
namespace ConsoleApplicationCourseTest{
class MainClass{
public class Person : ICloneable{
public string Name;
object ICloneable.Clone(){
return this.Clone();}
public Person Clone(){
return (Person)this.MemberwiseClone();}
}public static void Main(String[] args){
Person joe = new Person();joe.Name = "Joe Smith";
Person bob = (Person)joe.Clone();Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
bob.Name = "Bob PopUp";Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
}}
}
joe FullName: Joe Smithbob FullName: Joe Smithjoe FullName: Joe Smithbob FullName: Bob PopUpPress any key to continue...
namespace ConsoleApplicationCourseTest{
class MainClass{
public class Person : ICloneable{
public string Name;
object ICloneable.Clone(){
return this.Clone();}
public Person Clone(){
return (Person)this.MemberwiseClone();}
}public static void Main(String[] args){
Person joe = new Person();joe.Name = "Joe Smith";
Person bob = (Person)joe.Clone();Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
bob.Name = "Bob PopUp";Console.WriteLine("joe FullName: {0} \nbob FullName: {1}", joe.Name, bob.Name);
}}
}
joe FullName: Joe Smithbob FullName: Joe Smithjoe FullName: Joe Smithbob FullName: Bob PopUpPress any key to continue...
But this is all a headache!
The fastest way to deep clone..
The fastest way to deep clone
The fastest way to deep clone[Serialization]
public static class ObjectCopier{
/// <summary>/// Perform a deep Copy of the object./// </summary>/// <typeparam name="T">The type of object being copied.</typeparam>/// <param name="source">The object instance to copy.</param>/// <returns>The copied object.</returns>public static T Clone<T>(T source){
if (!typeof(T).IsSerializable){
throw new ArgumentException("The type must be serializable.", "source");}
// Don't serialize a null object, simply return the default for that objectif (Object.ReferenceEquals(source, null)){
return default(T);}
IFormatter formatter = new BinaryFormatter();Stream stream = new MemoryStream();using (stream){
formatter.Serialize(stream, source);stream.Seek(0, SeekOrigin.Begin);return (T)formatter.Deserialize(stream);
}}
}
public static class ObjectCopier{
/// <summary>/// Perform a deep Copy of the object./// </summary>/// <typeparam name="T">The type of object being copied.</typeparam>/// <param name="source">The object instance to copy.</param>/// <returns>The copied object.</returns>public static T Clone<T>(T source){
if (!typeof(T).IsSerializable){
throw new ArgumentException("The type must be serializable.", "source");}
// Don't serialize a null object, simply return the default for that objectif (Object.ReferenceEquals(source, null)){
return default(T);}
IFormatter formatter = new BinaryFormatter();Stream stream = new MemoryStream();using (stream){
formatter.Serialize(stream, source);stream.Seek(0, SeekOrigin.Begin);return (T)formatter.Deserialize(stream);
}}
}
public static class ObjectCopier{
/// <summary>/// Perform a deep Copy of the object./// </summary>/// <typeparam name="T">The type of object being copied.</typeparam>/// <param name="source">The object instance to copy.</param>/// <returns>The copied object.</returns>public static T Clone<T>(T source){
if (!typeof(T).IsSerializable){
throw new ArgumentException("The type must be serializable.", "source");}
// Don't serialize a null object, simply return the default for that objectif (Object.ReferenceEquals(source, null)){
return default(T);}
IFormatter formatter = new BinaryFormatter();Stream stream = new MemoryStream();using (stream){
formatter.Serialize(stream, source);stream.Seek(0, SeekOrigin.Begin);return (T)formatter.Deserialize(stream);
}}
}
public static class ObjectCopier{
/// <summary>/// Perform a deep Copy of the object./// </summary>/// <typeparam name="T">The type of object being copied.</typeparam>/// <param name="source">The object instance to copy.</param>/// <returns>The copied object.</returns>public static T Clone<T>(T source){
if (!typeof(T).IsSerializable){
throw new ArgumentException("The type must be serializable.", "source");}
// Don't serialize a null object, simply return the default for that objectif (Object.ReferenceEquals(source, null)){
return default(T);}
IFormatter formatter = new BinaryFormatter();Stream stream = new MemoryStream();using (stream){
formatter.Serialize(stream, source);stream.Seek(0, SeekOrigin.Begin);return (T)formatter.Deserialize(stream);
}}
}
public static class ObjectCopier{
/// <summary>/// Perform a deep Copy of the object./// </summary>/// <typeparam name="T">The type of object being copied.</typeparam>/// <param name="source">The object instance to copy.</param>/// <returns>The copied object.</returns>public static T Clone<T>(T source){
if (!typeof(T).IsSerializable){
throw new ArgumentException("The type must be serializable.", "source");}
// Don't serialize a null object, simply return the default for that objectif (Object.ReferenceEquals(source, null)){
return default(T);}
IFormatter formatter = new BinaryFormatter();Stream stream = new MemoryStream();using (stream){
formatter.Serialize(stream, source);stream.Seek(0, SeekOrigin.Begin);return (T)formatter.Deserialize(stream);
}}
}
public static class ObjectCopier{
/// <summary>/// Perform a deep Copy of the object./// </summary>/// <typeparam name="T">The type of object being copied.</typeparam>/// <param name="source">The object instance to copy.</param>/// <returns>The copied object.</returns>public static T Clone<T>(T source){
if (!typeof(T).IsSerializable){
throw new ArgumentException("The type must be serializable.", "source");}
// Don't serialize a null object, simply return the default for that objectif (Object.ReferenceEquals(source, null)){
return default(T);}
IFormatter formatter = new BinaryFormatter();Stream stream = new MemoryStream();using (stream){
formatter.Serialize(stream, source);stream.Seek(0, SeekOrigin.Begin);return (T)formatter.Deserialize(stream);
}}
}
End of CourseGo and have some fun and come back for the next
Advanced C# Course
Take a Look on my other courses @ http://www.slideshare.net/ZGTRZGTR
Available courses to the date of this slide:
http://www.mohammadshaker.com
https://twitter.com/ZGTRShaker @ZGTRShaker
https://de.linkedin.com/pub/mohammad-shaker/30/122/128/
http://www.slideshare.net/ZGTRZGTR
https://www.goodreads.com/user/show/11193121-mohammad-shaker
https://plus.google.com/u/0/+MohammadShaker/
https://www.youtube.com/channel/UCvJUfadMoEaZNWdagdMyCRA
http://mohammadshakergtr.wordpress.com/
Top Related