Often you have to implement a collection because it is not present among those of the .NET Framework. In the examples that I find online, often the new collection is built based on another collection (for example, List<T>): in this way it is possible to avoid the management of the resizing of the collection.
public class CustomCollection<T>
{
private List<T> _baseArray;
...
public CustomCollection(...)
{
this._baseArray = new List<T>(...);
}
}
- What are the disadvantages of following this approach? Only lower performance because of the method calls to the base collection? Or the compiler performs some optimization?
- Moreover, in some cases the field relating to the base collection (for example the above
_baseArray) is declared asreadonly. Why?
Usually it’s best to inherit from one of the many built-in collection classes to make your own collection, instead of doing it the hard way.
Collection<T>is a good starting point, and nobody is stopping you from inheritingList<T>itself.