123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ClonablePoint { class Program { static void Main(string[] args) { Point p1 = new Point(); Point p2 = (Point)p1.Clone(); // <<< clone of p1 in the heap!!! } } public class Point:ICloneable { public int X { get; set; } public int Y { get; set; } public Point(int x, int y) { X = x;Y = y; } public Point() { } public override string ToString() { //return base.ToString(); return string.Format("X={0},Y={1}", X, Y); } public object Clone() { // return new Point(this.X, this.Y); return this.MemberwiseClone(); // if doesn't exist refs to other objs // throw new NotImplementedException(); } } } |