// This abstract class doesn't need to implement Clone() because no instances of it // can ever be created, on account of it being abstract and all that. // If you COULD, it would look like this (but you can't so this won't compile): // public override object Clone() // { // return new AbstractDerived(this); // } // Implement copying fields in a private non-virtual method, called from more than one place. private void copyFrom(AbstractDerived other) // Other could be null, so check for nullness. { if (other != null) { this.ShortValue = other.ShortValue; } }
// Comparison. public override bool Equals(object obj) { if (object.ReferenceEquals(this, obj)) { return(true); } if (!base.Equals(obj)) { return(false); } AbstractDerived other = (AbstractDerived)obj; // This must succeed because if the types are different, base.Equals() returns false. return(this.IntValue == other.IntValue); }
// Copy constructor. protected AbstractDerived(AbstractDerived other) : base(other) { this.copyFrom(other); }