Why not inherit from List?

前端 未结 27 1774
悲哀的现实
悲哀的现实 2020-11-21 05:39

When planning out my programs, I often start with a chain of thought like so:

A football team is just a list of football players. Therefore, I should

27条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-21 05:56

    What is the correct C# way of representing a data structure...

    Remeber, "All models are wrong, but some are useful." -George E. P. Box

    There is no a "correct way", only a useful one.

    Choose one that is useful to you and/your users. That's it. Develop economically, don't over-engineer. The less code you write, the less code you will need to debug. (read the following editions).

    -- Edited

    My best answer would be... it depends. Inheriting from a List would expose the clients of this class to methods that may be should not be exposed, primarily because FootballTeam looks like a business entity.

    -- Edition 2

    I sincerely don't remember to what I was referring on the “don't over-engineer” comment. While I believe the KISS mindset is a good guide, I want to emphasize that inheriting a business class from List would create more problems than it resolves, due abstraction leakage.

    On the other hand, I believe there are a limited number of cases where simply to inherit from List is useful. As I wrote in the previous edition, it depends. The answer to each case is heavily influenced by both knowledge, experience and personal preferences.

    Thanks to @kai for helping me to think more precisely about the answer.

提交回复
热议问题