C# Starter L07-Objects Cloning

40
Mohammad Shaker mohammadshaker.com C# Programming Course @ZGTRShaker 2011, 2012, 2013, 2014 C# Starter L07 – Objects Cloning

description

C# Starter L07-Objects Cloning

Transcript of C# Starter L07-Objects Cloning

Page 1: C# Starter L07-Objects Cloning

Mohammad Shakermohammadshaker.com

C# Programming Course@ZGTRShaker

2011, 2012, 2013, 2014

C# StarterL07 – Objects Cloning

Page 2: C# Starter L07-Objects Cloning

uses Object Cloning to save the whole engine stateThe user can then switch back to any point in time!

Page 3: C# Starter L07-Objects Cloning

See more of @ http://mohammadshakergtr.wordpress.com/

and @ http://www.youtube.com/watch?v=FM3v0tbdKrs

Page 4: C# Starter L07-Objects Cloning

Cloning Objects – The Concept

Page 5: C# Starter L07-Objects Cloning

Shallow Clone VS Deep Clone

Page 6: C# Starter L07-Objects Cloning

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);

}}

}

Page 7: C# Starter L07-Objects Cloning

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...

Page 8: C# Starter L07-Objects Cloning

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...

Page 9: C# Starter L07-Objects Cloning

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...

Page 10: C# Starter L07-Objects Cloning

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...

Page 11: C# Starter L07-Objects Cloning

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...

Page 12: C# Starter L07-Objects Cloning

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...

Page 13: C# Starter L07-Objects Cloning

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...

Page 14: C# Starter L07-Objects Cloning

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...

Page 15: C# Starter L07-Objects Cloning

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...

Page 16: C# Starter L07-Objects Cloning

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...

Page 17: C# Starter L07-Objects Cloning

Try to Clone with IClonable interface

Page 18: C# Starter L07-Objects Cloning

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();

Page 19: C# Starter L07-Objects Cloning

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);

}}

}

Page 20: C# Starter L07-Objects Cloning

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);

}}

}

Page 21: C# Starter L07-Objects Cloning

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);

}}

}

Page 22: C# Starter L07-Objects Cloning

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);

}}

}

Page 23: C# Starter L07-Objects Cloning

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);

}}

}

Page 24: C# Starter L07-Objects Cloning

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);

}}

}

Page 25: C# Starter L07-Objects Cloning

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...

Page 26: C# Starter L07-Objects Cloning

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...

Page 27: C# Starter L07-Objects Cloning

But this is all a headache!

Page 28: C# Starter L07-Objects Cloning

The fastest way to deep clone..

Page 29: C# Starter L07-Objects Cloning

The fastest way to deep clone

Page 30: C# Starter L07-Objects Cloning

The fastest way to deep clone[Serialization]

Page 31: C# Starter L07-Objects Cloning

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);

}}

}

Page 32: C# Starter L07-Objects Cloning

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);

}}

}

Page 33: C# Starter L07-Objects Cloning

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);

}}

}

Page 34: C# Starter L07-Objects Cloning

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);

}}

}

Page 35: C# Starter L07-Objects Cloning

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);

}}

}

Page 36: C# Starter L07-Objects Cloning

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);

}}

}

Page 37: C# Starter L07-Objects Cloning

End of CourseGo and have some fun and come back for the next

Advanced C# Course

Page 38: C# Starter L07-Objects Cloning

Take a Look on my other courses @ http://www.slideshare.net/ZGTRZGTR

Available courses to the date of this slide:

Page 39: C# Starter L07-Objects Cloning

http://www.mohammadshaker.com

[email protected]

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/

Page 40: C# Starter L07-Objects Cloning