Search This Blog

Tuesday, June 7, 2016

Deep clone example

Deep-cloning example:

 public class RechivHafrasha : IEqualityComparer<RechivHafrasha>
    {
        public decimal Schum { getset; }
        public decimal? Shiur { getset; }
 
        //public int? Status { get; private set; }
        public int Sug { getset; }
 
        public decimal Sachar { getprivate set; }
        public string MisparMezaheReshuma { getset; }
 
        public string MisparMezaheReshumaKodem { getset; }
 
        public RechivHafrasha()
        {
        }
 
        public RechivHafrasha(decimal schumHafrasha, decimal? shiurHafrasha, /*int? status,*/ int sugHafrasha, decimal sachar, string misparMezaheReshuma, string misparMezaheReshumaKodem)
        {
            Schum = schumHafrasha;
            Shiur = shiurHafrasha;
            //Status = status;
            Sug = sugHafrasha;
            Sachar = sachar;
            MisparMezaheReshuma = misparMezaheReshuma;
            MisparMezaheReshumaKodem = misparMezaheReshumaKodem;
        }
 
        /*
        * לצורך השוואת בין רשומות מאוחדות, כדי לא לשלוח כפילויות לפירעון
        */
 
        public string ID
        {
            get
            {
                return string.Format("{0}{1}{2}{3}{4}{5}",
                    this.Schum,
                    this.Shiur,
                    this.Sug,
                    this.Sachar,
                    this.MisparMezaheReshuma,
                    this.MisparMezaheReshumaKodem);
            }
        }
 
        public bool Equals(RechivHafrasha x, RechivHafrasha y)
        {
            return x.ID.Equals(y.ID);
        }
 
        public int GetHashCode(RechivHafrasha obj)
        {
            return obj.ID.GetHashCode();
        }
 
        public RechivHafrasha ShallowCopy()
        {
            var shallowCopy = this.MemberwiseClone() as RechivHafrasha;
            return shallowCopy;
        }
 
        public override string ToString()
        {
            return string.Format("[{0},{1},{2},{3},{4},{5}]", Schum, Shiur, Sug, Sachar, MisparMezaheReshuma, MisparMezaheReshumaKodem);
        }
    }
 
 
 
 
            List<RechivHafrasha> list = new List<RechivHafrasha>
            {
                new RechivHafrasha(10, 0.5m, 1, 1000, "7d2d272d7268326""2d2862g2g2d"),
                new RechivHafrasha(100, 0.7m, 2, 900, "7d2d272d7268326""2d2862g2g2d"),
                new RechivHafrasha(1000, 0.2m, 5, 80, "7d2d272d7268326""2d2862g2g2d"),
                new RechivHafrasha(10000, 0.41m, 4, 500, "7d2d272d7268326""2d2862g2g2d"),
                new RechivHafrasha(100000, 0.11m, 1, 15, "7d2d272d7268326""2d2862g2g2d")
            };
 
            List<RechivHafrasha> c1 = list.ToList();
            List<RechivHafrasha> c2 = new List<RechivHafrasha>(list);
            List<RechivHafrasha> c3 = list.ConvertAll<RechivHafrasha>(z => z.ShallowCopy());
 
            list[1].Schum = 0;
 
            Console.WriteLine(string.Join(",\n", list));
            Console.WriteLine();
            Console.WriteLine(string.Join(",\n", c1));
            Console.WriteLine();
            Console.WriteLine(string.Join(",\n", c2));
            Console.WriteLine();
            Console.WriteLine(string.Join(",\n", c3)); 
Output:

No comments:

Post a Comment