// Implement copying fields in a private non-virtual method, called from more than one place. private void copyFrom(Derived1 other) // Other could be null, so check for nullness. { if (other != null) { this.StringValue = other.StringValue; } }
// Equality check. public override bool Equals(object obj) { if (object.ReferenceEquals(this, obj)) { return(true); } if (!base.Equals(obj)) { return(false); } Derived1 other = (Derived1)obj; // This must succeed because if the types are different, base.Equals() returns false. return(this.StringValue == other.StringValue); }
// Protected copy constructor. Used to implement Clone(). // Also called by a derived class' copy constructor. protected Derived1(Derived1 other) : base(other) { this.copyFrom(other); }