• How to create a collection class for custom class

    SapnaVishwas Member

    Simply inherits from List and your collection will be a List with everything already built-in, including the implementation of GetEnumerator.

  • ShikhaTan Member

    Just look at the official documentation for List. The documentation often tells you more than what you read here and there, and you can usually count on it to be right. The Internet is full of false statements about everything.

    Look at the Syntax portion of the documentation page, and you will see that List implements IEnumerable, meaning that it already did the job for you.

    If you still do not believe me, simply try a foreach on your class. Even without data in it, you will see that the syntax does not generate an error. It would give you an error just as your initial class if it did not implement GetEnumerator.

    But change something in your last code. Because you inherits from List, your class is a List. You do not need to an internal list, you do not need to add anything:

        public class DataExtractTablesColl : List
        {
        }
    

    You would add something if you wanted special constructors, supplementary methods and properties, or change the way some of the properties and methods work. Otherwise, inheriting from a class usually automatically gives you all its features. That is the first aim of inheritance.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.
en_USEnglish