When we create an object by utilizing new key phrase, it takes reminiscence. If we create 1000 objects in a pc, we don’t care about its reminiscence. Because we all know that, laptop has lot of reminiscence. But take into consideration cellular. Its reminiscence could be very low. If we create lot of objects for a cellular recreation, it should come up downside.

In this example prototype sample will assist use. We will make a clone or prototype of a specific object. When we want that object, we’ll use the clone object. Every time we don’t have to create new object.

Consider the bellow Customer class.

public Customer Person
 {
        public String Name { get; set; }
        public String Email { get; set; }
 }
public static void Main(string[] args)
 {
      var buyer= new Customer();
      buyer.Name = "John Abrar";
      buyer.Email = "[email protected]";
      Console.WriteLine("Name:{0} and Email:{1}", buyer.Name, buyer.Email);
 
 }

Now if we have to use this Customer class in 1000 locations, we have now to create 1000 objects. This is just not good follow.

Let’s clone the Customer class.

public interface IClone
 {
   Object Clone();
 }
public class Customer : IClone
 {
     public String Name { get; set; }
     public String Email { get; set; }
     public Object Clone()
     {
          var buyer= new Customer 
          {
                Name = Name,
                Email = Email
          };
 
            return buyer;
     }
 }
public class Program
 {
        public static void Main(string[] args)
        {
            Customer buyer = new Customer();
            buyer.Name = "John Abrar";
            buyer.Email = "[email protected]";
 
            Console.WriteLine("Before Cloning.......");
            Console.WriteLine("Name:{0} and Email:{1}", buyer.Name, buyer.Email);
 
            Customer customer1= buyer.Clone() as Customer;
            Console.WriteLine("After Cloning..............");
            Console.WriteLine("Name:{0} and Email:{1}", customer1.Name, customer1.Email);
        }
 }

Source link