Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Customer
- {
- public int Id;
- public string Name;
- public Customer CreateShallowCopy()
- {
- return (Customer)this.MemberwiseClone();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Customer objCustomer = new Customer() { Id = 1, Name = "James"};
- Customer objCustomer2 = objCustomer;
- Customer objCustomerShallowCopy = objCustomer.CreateShallowCopy();
- objCustomer.Name = "Jim";
- objCustomer.Id = 2;
- }
- }
- public static T DeepCopy<T>(T item)
- {
- BinaryFormatter formatter = new BinaryFormatter();
- MemoryStream stream = new MemoryStream();
- formatter.Serialize(stream, item);
- stream.Seek(0, SeekOrigin.Begin);
- T result = (T)formatter.Deserialize(stream);
- stream.Close();
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement