public object Clone() { Point newPoint = (Point)this.MemberwiseClone(); PointDescription currentDesc = new PointDescription(); currentDesc.petName = this.desc.petName; newPoint.desc = currentDesc; return(newPoint); }
public object Clone() { Point newPoint = (Point) this.MemberwiseClone(); PointDescription currentDesc = new PointDescription(); currentDesc.PetName = this.desc.PetName; newPoint.desc = currentDesc; return newPoint; }
public object Clone() { //return new Point(this.X, this.Y); Point newPoint = (Point) this.MemberwiseClone(); PointDescription newDesc = new PointDescription(); newDesc.PetName = this.desc.PetName; newPoint.desc = newDesc; return newPoint; }
public object Clone() { Point newPoint = (Point)MemberwiseClone(); PointDescription newDesc = new PointDescription(); newDesc.PetName = desc.PetName; newPoint.desc = newDesc; return(newPoint); }
// Return a copy of the current object. public object Clone() { // First get a shallow copy. Point newPoint = (Point)this.MemberwiseClone(); // Then fill in the gaps. PointDescription currentDesc = new PointDescription(); currentDesc.PetName = this.desc.PetName; newPoint.desc = currentDesc; return(newPoint); }
// Deep copy clone public object Clone() { // First get a shallow copy. Point newPoint = (Point)this.MemberwiseClone(); // Then fill in the gaps. PointDescription currentDesc = new PointDescription(); currentDesc.PetName = this.desc.PetName; newPoint.desc = currentDesc; return newPoint; }
//return a copy of the current object public object Clone() { //return new Point(this.X, this.Y);//alternatively.. //return this.MemberwiseClone();//this only works bc Point doesn't contain any ref type member variables Point newPoint = (Point)this.MemberwiseClone();//"shallow copy" //filling in all the gaps from shallow copy... PointDescription currentDesc = new PointDescription(); currentDesc.PetName = this.desc.PetName; newPoint.desc = currentDesc; return newPoint; }
// 返回当前对象的副本 public object Clone() { // 首先获取签复制 Point newPoint = (Point)this.MemberwiseClone(); //然后填充间距 PointDescription currentDesc = new PointDescription(); currentDesc.PetName = this.desc.PetName; newPoint.desc = currentDesc; return(newPoint); }
// 对于内部有引用类型的自定义类来说,为了实现真正的深拷贝,你需要使用MemberwiseClone返回值 // 去配置对象 public object Clone() { //return new Point(this.X, this.Y); // 因为这个Point类型中不包含任何引用类型变量,所以你可以使用MemberwiseClone //return this.MemberwiseClone(); // 但是如果类中有引用类型的成员的话,那个MemberwiseClone会通过浅拷贝,那些引 // 用类型 Point newPoint = (Point)this.MemberwiseClone(); PointDescription currenDes = new PointDescription(); currenDes.PetName = this.desc.PetName; newPoint.desc = currenDes; return newPoint; }
public object Clone() { //return new Point(this.X, this.Y); // copy each field of the Point member by member //return this.MemberwiseClone(); Point newPoint = (Point)this.MemberwiseClone(); PointDescription currentDesc = new PointDescription(); currentDesc.PetName = this.desc.PetName; newPoint.desc = currentDesc; return(newPoint); }
public object Clone() { //return new Point(this.X, this.Y); //return this.MemberwiseClone(); // first a shallow copy Point newPoint = (Point)this.MemberwiseClone(); // then fill in the gaps PointDescription currentDesc = new PointDescription(); currentDesc.PetName = this.desc.PetName; newPoint.desc = currentDesc; return(newPoint); }
// Return a copy of the current object. // Now we need to adjust for the PointDescription member. public object Clone() { // First get a shallow copy. Point newPoint = (Point)this.MemberwiseClone(); // Then fill in the gaps. // COMMENT THIS OUT TO SHOW SHALLOW COPY EXAMPLE PointDescription currentDesc = new PointDescription { PointName = this.desc.PointName }; newPoint.desc = currentDesc; return(newPoint); }
public object Clone() { //Use this if the object cntains only value types //return this.MemberwiseClone(); //if the object contains reference types then you have to fill them in //First get shallow copy Point newPoint = (Point)this.MemberwiseClone(); //then fill in the gap PointDescription currentDesc = new PointDescription(); currentDesc.PetName = this.desc.PetName; newPoint.desc = currentDesc; return(newPoint); }