There is a user class inheriting from List

class EpochCollection:List<Epoch> 

How can I correctly implement the GetRange (int, int) method GetRange (int, int) in the EpochCollection so that it creates a new instance of the EpochCollection , with the part corresponding to the list cut off according to the specified bounds, and took the part of the class that I defined in the EpochCollection from the original object without modification?

Or, instead of inheriting from List preferable to describe your own collection, just as described here .

  • one
    in general, it is recommended not to inherit from collections, but to include them as fields - Grundy
  • How acceptable is the following implementation: public EpochCollection GetRange (int index, int count) {Res = (EpochCollection) this.Clone (); Res.Clear (); for (int i = index; i <count; i ++) Res. Add (this [i]); return res; } - Vasliy
  • and what is the meaning of cloning and subsequent purification if then again filled? well, all the necessary code should be added to the question - Grundy

1 answer 1

 public class EpochCollection : IEnumerable<Epoch> { private List<Epoch> _source; private EpochCollection(List<Epoch> source) { _source = source; } public EpochCollection GetRange(int index, int count) { return new EpochCollection(_source.GetRange(index, count)); } public IEnumerator<Epoch> GetEnumerator() { return _source.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return _source.GetEnumerator(); } } 

Add the necessary public constructors and methods.