Пример #1
0
 // Implement copying fields in a private non-virtual method, called from more than one place.
 private void copyFrom(Derived2 other)      // Other could be null, so check for nullness.
 {
     if (other != null)
     {
         this.DoubleValue = other.DoubleValue;
     }
 }
Пример #2
0
 // Protected copy constructor. Used to implement Clone().
 // Also called by a derived class' copy constructor.
 protected Derived2(Derived2 other) : base(other)
 {
     // Canonical implementation: use ":base(other)" to copy all
     // the base fields (which recursively applies all the way to the ultimate base)
     // and then explicitly copy any of this class' fields here:
     this.copyFrom(other);
 }
Пример #3
0
        static Derived2 Clone(AbstractBase item)
        {
            AbstractBase abstractBase = (AbstractBase)item.Clone();
            Derived2     result       = abstractBase as Derived2;

            Debug.Assert(result != null);
            return(result);
        }
Пример #4
0
        static void Main()
        {
            Derived2 test = new Derived2()
            {
                IntValue    = 1,
                StringValue = "s",
                DoubleValue = 2,
                ShortValue  = 3
            };
            Derived2 copy = Clone(test);

            Console.WriteLine(copy);
        }
Пример #5
0
        // Equality check.
        public override bool Equals(object obj)
        {
            if (object.ReferenceEquals(this, obj))
            {
                return(true);
            }
            if (!base.Equals(obj))
            {
                return(false);
            }
            Derived2 other = (Derived2)obj;      // This must succeed because if the types are different, base.Equals() returns false.

            return(this.DoubleValue == other.DoubleValue);
        }