How do I copy an instance of an object?

后端 未结 4 1830
天命终不由人
天命终不由人 2020-12-30 05:22

I\'m trying to write some code that populates a List (actually, it\'s a series of Lists, but we can pretend it\'s just one List). The

4条回答
  •  猫巷女王i
    2020-12-30 06:15

    If you only need a shallow copy, then you can write a quick-fix clone method:

    public class PinnacleStock : ICloneable
    {
        public PinnacleStock Clone()
        {
            return (PinnacleStock)this.MemberwiseClone();
        }
    
        object ICloneable.Clone()
        {
            return Clone();
        }
    
        // Other methods
    }
    

    If you need a deep copy (i.e. if PinnacleStock has sub-objects that you want to be copied as well), then you will need to write one yourself.

提交回复
热议问题