Why not inherit from List?

前端 未结 27 1809
悲哀的现实
悲哀的现实 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:54

    Problems with serializing

    One aspect is missing. Classes that inherit from List<> can't be serialized correctly using XmlSerializer. In that case DataContractSerializer must be used instead, or an own serializing implementation is needed.

    public class DemoList : List
    {
        // using XmlSerializer this properties won't be seralized:
        string AnyPropertyInDerivedFromList { get; set; }     
    }
    
    public class Demo
    {
        // this properties will be seralized
        string AnyPropetyInDemo { get; set; }  
    }
    

    Further reading: When a class is inherited from List<>, XmlSerializer doesn't serialize other attributes

提交回复
热议问题