Avatar ·

Why you shouldn't inherit from List<T> in C#? Alternatives and best practices

Hello everyone! I'm a beginner C# developer and I've encountered a situation where I need to create my own class that behaves like a list but with additional logic. For example, I need to store a list of orders, and I want the total sum to be automatically recalculated when a new order is added. I thought the simplest way would be to inherit from `List` and add my own methods. Here's what I came up with: ```csharp public class OrderList : List { public decimal TotalSum { get; private set; } public new void Add(Order order) { base.Add(order); TotalSum += order.Price; } public new bool Remove(Order order) { bool result = base.Remove(order); if (result) TotalSum -= order.Price;