Saturday, 2 July 2016

c# - Classes with Collections as Properties vs. Classes Inheriting Collections



Recently I used a class that inherits from a collection instead of having the collection instantiated within the class, is this acceptable or does it create unseen problems further down the road? Examples below for the sake of clarity:



public class Cars : List


instead of something like:




public class Cars
{
List CarList = new List();
}


Any thoughts?


Answer



The problem with this is that your Cars class will still have the interface it inherits from List, which may allow operations you don't want.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...