Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. public class Customer
  2. {
  3.  
  4. public int Id;
  5. public string Name;
  6.  
  7. public Customer CreateShallowCopy()
  8. {
  9. return (Customer)this.MemberwiseClone();
  10. }
  11. }
  12.  
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. Customer objCustomer = new Customer() { Id = 1, Name = "James"};
  18. Customer objCustomer2 = objCustomer;
  19.  
  20. Customer objCustomerShallowCopy = objCustomer.CreateShallowCopy();
  21.  
  22. objCustomer.Name = "Jim";
  23. objCustomer.Id = 2;
  24. }
  25. }
  26.  
  27. public static T DeepCopy<T>(T item)
  28. {
  29. BinaryFormatter formatter = new BinaryFormatter();
  30. MemoryStream stream = new MemoryStream();
  31. formatter.Serialize(stream, item);
  32. stream.Seek(0, SeekOrigin.Begin);
  33. T result = (T)formatter.Deserialize(stream);
  34. stream.Close();
  35. return result;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement