Avatar ·

Why you shouldn't inherit your class from List<T> in C#?

Hello everyone! I'm a beginner C# developer and I've encountered a design problem. I'm writing an application for library management. I have a `Book` class and I want to create a `Library` class that would contain a list of books and also have its own methods, for example, `FindByAuthor()` or `GetTotalPages()`. At first, I thought the most logical thing was to simply inherit from `List`: ```csharp public class Library : List { public List FindByAuthor(string author) { return this.Where(b => b.Author == author).ToList(); } public int GetTotalPages() { return this.Sum(b => b.Pages); } } ``` It seems to work, but I read on Stack Overflow that this is bad practice. People write that "you shouldn't inherit from List". But I