SlideShare a Scribd company logo
Mohammad Shaker
mohammadshaker.com
C# Programming Course
@ZGTRShaker
2011, 2012, 2013, 2014
C# Starter
L07 – Objects Cloning
uses Object Cloning to save the whole engine state
The 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. 0000
M2 Num. 0000
Press 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. 0000
M2 Num. 0000
Press 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. 0000
M2 Num. 0000
Press 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. 0000
M2 Num. 0000
Press 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. 0000
M2 Num. 0000
Press 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. 0000
M2 Num. 0000
Press 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. 0000
M2 Num. 0000
Press 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. 0000
M2 Num. 0000
Press 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. 0000
M2 Num. 0000
Press 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. 0000
M2 Num. 0000
Press 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 Smith
bob FullName: Joe Smith
joe FullName: Joe Smith
bob FullName: Bob PopUp
Press 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 Smith
bob FullName: Joe Smith
joe FullName: Joe Smith
bob FullName: Bob PopUp
Press 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 object
if (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 object
if (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 object
if (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 object
if (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 object
if (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 object
if (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 Course
Go 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
mohammadshakergtr@gmail.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/
C# Starter L07-Objects Cloning

More Related Content

PDF
Aplikasi rawat-inap-vbnet
PDF
C++ Windows Forms L09 - GDI P2
PDF
Java File
PDF
Chat application in java using swing and socket programming.
DOCX
Java practical
PDF
Sam wd programs
PDF
Ditec esoft C# project
Aplikasi rawat-inap-vbnet
C++ Windows Forms L09 - GDI P2
Java File
Chat application in java using swing and socket programming.
Java practical
Sam wd programs
Ditec esoft C# project

What's hot (20)

PDF
Ditec esoft C# project
PDF
The art of reverse engineering flash exploits
PDF
Advanced Java Practical File
PDF
C++ Windows Forms L02 - Controls P1
PPS
Class method
PDF
C++ Windows Forms L11 - Inheritance
DOC
Ad java prac sol set
PDF
Chat Room System using Java Swing
PDF
C++ Windows Forms L05 - Controls P4
PDF
final project for C#
PDF
Vb.net iv
PPTX
Java Programs
PDF
Kotlin puzzler
DOCX
JDBC (JAVA Database Connectivity)
PPTX
Python functions
PDF
Bot builder v4 HOL
DOC
Advanced Java - Praticals
DOCX
Advance Java Programs skeleton
PDF
Java_practical_handbook
PDF
Important java programs(collection+file)
Ditec esoft C# project
The art of reverse engineering flash exploits
Advanced Java Practical File
C++ Windows Forms L02 - Controls P1
Class method
C++ Windows Forms L11 - Inheritance
Ad java prac sol set
Chat Room System using Java Swing
C++ Windows Forms L05 - Controls P4
final project for C#
Vb.net iv
Java Programs
Kotlin puzzler
JDBC (JAVA Database Connectivity)
Python functions
Bot builder v4 HOL
Advanced Java - Praticals
Advance Java Programs skeleton
Java_practical_handbook
Important java programs(collection+file)
Ad

Viewers also liked (18)

PDF
Indie Series 03: Becoming an Indie
PDF
XNA L11–Shaders Part 2
PDF
Delphi L02 Controls P1
PDF
XNA L10–Shaders Part 1
PDF
Utilizing Kinect Control for a More Immersive Interaction with 3D Environment
PDF
C# Advanced L09-HTML5+ASP
PDF
WPF L03-3D Rendering and 3D Animation
PDF
Android L01 - Warm Up
PDF
C# Advanced L10-Workflow Foundation
PDF
WPF L01-Layouts, Controls, Styles and Templates
PDF
Interaction Design L06 - Tricks with Psychology
PDF
C# Advanced L08-Networking+WCF
PDF
Short, Matters, Love - Passioneers Event 2015
PDF
C# Starter L06-Delegates, Event Handling and Extension Methods
PDF
OpenGL Starter L02
PDF
Car Dynamics with ABS, ESP and GPS Systems
PDF
XNA L02–Basic Matrices and Transformations
PDF
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Indie Series 03: Becoming an Indie
XNA L11–Shaders Part 2
Delphi L02 Controls P1
XNA L10–Shaders Part 1
Utilizing Kinect Control for a More Immersive Interaction with 3D Environment
C# Advanced L09-HTML5+ASP
WPF L03-3D Rendering and 3D Animation
Android L01 - Warm Up
C# Advanced L10-Workflow Foundation
WPF L01-Layouts, Controls, Styles and Templates
Interaction Design L06 - Tricks with Psychology
C# Advanced L08-Networking+WCF
Short, Matters, Love - Passioneers Event 2015
C# Starter L06-Delegates, Event Handling and Extension Methods
OpenGL Starter L02
Car Dynamics with ABS, ESP and GPS Systems
XNA L02–Basic Matrices and Transformations
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ad

Similar to C# Starter L07-Objects Cloning (20)

PDF
C# Starter L02-Classes and Objects
PDF
iOS Automation Primitives
PDF
Owasp orlando, april 13, 2016
PPTX
Introduction to Java Programming
PDF
Java day 2016.pptx
PPTX
Kotlin for android
PPTX
c#(loops,arrays)
PDF
Object-oriented Basics
PPTX
Using Reflections and Automatic Code Generation
PPTX
War Simulation
DOCX
DOCX
Dotnet 18
PPTX
Ocl 09
PPTX
Kotlin / Android Update
PDF
PDF
Painless Persistence with Realm
PPTX
Java oops features
PDF
4Developers: Michał Szczepanik- Kotlin - Let’s ketchup it
PPTX
Intro to object oriented programming
PPTX
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
C# Starter L02-Classes and Objects
iOS Automation Primitives
Owasp orlando, april 13, 2016
Introduction to Java Programming
Java day 2016.pptx
Kotlin for android
c#(loops,arrays)
Object-oriented Basics
Using Reflections and Automatic Code Generation
War Simulation
Dotnet 18
Ocl 09
Kotlin / Android Update
Painless Persistence with Realm
Java oops features
4Developers: Michał Szczepanik- Kotlin - Let’s ketchup it
Intro to object oriented programming
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...

More from Mohammad Shaker (20)

PDF
12 Rules You Should to Know as a Syrian Graduate
PDF
Unity L01 - Game Development
PDF
Android L07 - Touch, Screen and Wearables
PDF
Interaction Design L03 - Color
PDF
Interaction Design L05 - Typography
PDF
Interaction Design L04 - Materialise and Coupling
PDF
Android L05 - Storage
PDF
Android L04 - Notifications and Threading
PDF
Android L09 - Windows Phone and iOS
PDF
Interaction Design L01 - Mobile Constraints
PDF
Interaction Design L02 - Pragnanz and Grids
PDF
Android L10 - Stores and Gaming
PDF
Android L06 - Cloud / Parse
PDF
Android L08 - Google Maps and Utilities
PDF
Android L03 - Styles and Themes
PDF
Android L02 - Activities and Adapters
PDF
Indie Series 01: Intro to Games
PDF
Indie Series 04: The Making of SyncSeven
PDF
Indie Series 02: AI and Recent Advances in Games
PDF
Roboconf DSL Advanced Software Engineering
12 Rules You Should to Know as a Syrian Graduate
Unity L01 - Game Development
Android L07 - Touch, Screen and Wearables
Interaction Design L03 - Color
Interaction Design L05 - Typography
Interaction Design L04 - Materialise and Coupling
Android L05 - Storage
Android L04 - Notifications and Threading
Android L09 - Windows Phone and iOS
Interaction Design L01 - Mobile Constraints
Interaction Design L02 - Pragnanz and Grids
Android L10 - Stores and Gaming
Android L06 - Cloud / Parse
Android L08 - Google Maps and Utilities
Android L03 - Styles and Themes
Android L02 - Activities and Adapters
Indie Series 01: Intro to Games
Indie Series 04: The Making of SyncSeven
Indie Series 02: AI and Recent Advances in Games
Roboconf DSL Advanced Software Engineering

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Transform Your Business with a Software ERP System
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
assetexplorer- product-overview - presentation
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Introduction to Artificial Intelligence
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
L1 - Introduction to python Backend.pptx
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
System and Network Administraation Chapter 3
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Transform Your Business with a Software ERP System
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Odoo Companies in India – Driving Business Transformation.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Computer Software and OS of computer science of grade 11.pptx
assetexplorer- product-overview - presentation
Which alternative to Crystal Reports is best for small or large businesses.pdf
Design an Analysis of Algorithms I-SECS-1021-03
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PTS Company Brochure 2025 (1).pdf.......
Introduction to Artificial Intelligence
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
L1 - Introduction to python Backend.pptx
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf

C# Starter L07-Objects Cloning

  • 1. Mohammad Shaker mohammadshaker.com C# Programming Course @ZGTRShaker 2011, 2012, 2013, 2014 C# Starter L07 – Objects Cloning
  • 2. uses Object Cloning to save the whole engine state The user can then switch back to any point in time!
  • 3. See more of @ http://mohammadshakergtr.wordpress.com/ and @ http://www.youtube.com/watch?v=FM3v0tbdKrs
  • 4. Cloning Objects – The Concept
  • 5. Shallow Clone VS Deep Clone
  • 6. 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); } } }
  • 7. 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. 0000 M2 Num. 0000 Press any key to continue...
  • 8. 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. 0000 M2 Num. 0000 Press any key to continue...
  • 9. 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. 0000 M2 Num. 0000 Press any key to continue...
  • 10. 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. 0000 M2 Num. 0000 Press any key to continue...
  • 11. 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. 0000 M2 Num. 0000 Press any key to continue...
  • 12. 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. 0000 M2 Num. 0000 Press any key to continue...
  • 13. 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. 0000 M2 Num. 0000 Press any key to continue...
  • 14. 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. 0000 M2 Num. 0000 Press any key to continue...
  • 15. 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. 0000 M2 Num. 0000 Press any key to continue...
  • 16. 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. 0000 M2 Num. 0000 Press any key to continue...
  • 17. Try to Clone with IClonable interface
  • 18. 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();
  • 19. 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); } } }
  • 20. 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); } } }
  • 21. 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); } } }
  • 22. 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); } } }
  • 23. 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); } } }
  • 24. 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); } } }
  • 25. 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 Smith bob FullName: Joe Smith joe FullName: Joe Smith bob FullName: Bob PopUp Press any key to continue...
  • 26. 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 Smith bob FullName: Joe Smith joe FullName: Joe Smith bob FullName: Bob PopUp Press any key to continue...
  • 27. But this is all a headache!
  • 28. The fastest way to deep clone..
  • 29. The fastest way to deep clone
  • 30. The fastest way to deep clone [Serialization]
  • 31. 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 object if (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); } } }
  • 32. 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 object if (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); } } }
  • 33. 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 object if (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); } } }
  • 34. 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 object if (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); } } }
  • 35. 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 object if (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); } } }
  • 36. 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 object if (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); } } }
  • 37. End of Course Go and have some fun and come back for the next Advanced C# Course
  • 38. Take a Look on my other courses @ http://www.slideshare.net/ZGTRZGTR Available courses to the date of this slide: