[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"doc-seo-685663-105":3,"detail-sidebar-cat-0-en-105":81,"doc-detail-685663-en":130,"doc-similar-685663":148},{"code":4,"msg":5,"data":6},0,"ok",{"site_id":7,"language":8,"slug":9,"title":10,"keywords":11,"description":12,"schema_data":13,"social_meta":74,"head_meta":76,"extra_data":78,"updated_unix":80},105,"en","more-effective-c-second-edition-item-14-prefer-defining-and-implementing-interfaces-to-inheritance","More Effective C# - Second Edition - Item 14 - Prefer Defining and Implementing Interfaces to Inheritance","","Prefer defining and implementing interfaces rather than relying on inheritance to extend behavior in C#. Adding functionality to a base class automatically affects all derived classes, but extending an interface breaks every implementing type and requires updates. When interfaces must evolve safely, define a new interface that inherits the existing one. The text contrasts fixed interface contracts with extendable base classes and explains how to combine interface contracts with reuse through patterns like IEnumerable\u003CT> and LINQ.",{"@graph":14,"@context":73},[15,34,56],{"@type":16,"itemListElement":17},"BreadcrumbList",[18,23,27,31],{"item":19,"name":20,"@type":21,"position":22},"https://docshare.wps.com","Home","ListItem",1,{"item":24,"name":25,"@type":21,"position":26},"https://docshare.wps.com/document/","Document",2,{"item":28,"name":29,"@type":21,"position":30},"https://docshare.wps.com/document/technology/","Technology",3,{"item":32,"name":10,"@type":21,"position":33},"https://docshare.wps.com/document/more-effective-c-second-edition-item-14-prefer-defining-and-implementing-interfaces-to-inheritance/685663/",4,{"url":32,"name":10,"@type":35,"image":36,"author":41,"headline":10,"publisher":44,"fileFormat":47,"inLanguage":8,"description":12,"dateModified":48,"datePublished":49,"encodingFormat":47,"isAccessibleForFree":50,"interactionStatistic":51},"DigitalDocument",{"url":37,"@type":38,"width":39,"height":40},"https://docshare.wps.com/thumbnails/more-effective-c-second-edition-item-14-prefer-defining-and-implementing-interfaces-to-inheritance/685663.png","ImageObject",300,407,{"name":42,"@type":43},"AfterMidnight","Person",{"url":19,"name":45,"@type":46},"DocShare","Organization","application/pdf","2026-11-16","2026-11-07",true,{"@type":52,"interactionType":53,"userInteractionCount":55},"InteractionCounter",{"@type":54},"ViewAction",7,{"@type":57,"mainEntity":58},"FAQPage",[59,65,69],{"name":60,"@type":61,"acceptedAnswer":62},"Why does adding members to an interface break implementing classes?","Question",{"text":63,"@type":64},"Because every implementing type must update to include the new member; otherwise the new method is missing and the code no longer compiles.","Answer",{"name":66,"@type":61,"acceptedAnswer":67},"How can you add functionality to an interface without breaking existing code?",{"text":68,"@type":64},"Create a new interface that inherits from the existing one, so existing implementations remain valid while new functionality is provided through the new contract.",{"name":70,"@type":61,"acceptedAnswer":71},"What does implementing IEnumerable\u003CT> enable for a type like WeatherDataStream?",{"text":72,"@type":64},"It enables LINQ extension methods defined for IEnumerable\u003CT>, allowing the type to act as a source for LINQ queries.","https://schema.org",{"og:url":32,"og:type":75,"og:title":10,"og:site_name":45,"og:description":12},"article",{"robots":77,"canonical":32},"index,follow",{"doc_id":79,"site_id":7},685663,1794016364,{"code":4,"msg":82,"data":83},"success",[84,88,92,96,101,105,109,114,119,122,126],{"id":22,"doc_module":4,"doc_module_name":25,"category_name":85,"show_sort_weight":86,"slug":87},"Story & Novel",90,"story-novel",{"id":26,"doc_module":4,"doc_module_name":25,"category_name":89,"show_sort_weight":90,"slug":91},"Literature",80,"literature",{"id":33,"doc_module":4,"doc_module_name":25,"category_name":93,"show_sort_weight":94,"slug":95},"Exam",70,"exam",{"id":97,"doc_module":4,"doc_module_name":25,"category_name":98,"show_sort_weight":99,"slug":100},5,"Comic",60,"comic",{"id":102,"doc_module":4,"doc_module_name":25,"category_name":29,"show_sort_weight":103,"slug":104},6,50,"technology",{"id":55,"doc_module":4,"doc_module_name":25,"category_name":106,"show_sort_weight":107,"slug":108},"Healthcare",40,"healthcare",{"id":110,"doc_module":4,"doc_module_name":25,"category_name":111,"show_sort_weight":112,"slug":113},8,"Research & Report",30,"research-report",{"id":115,"doc_module":4,"doc_module_name":25,"category_name":116,"show_sort_weight":117,"slug":118},9,"Religion & Spirituality",20,"religion-spirituality",{"id":117,"doc_module":4,"doc_module_name":25,"category_name":120,"show_sort_weight":117,"slug":121},"World Cup","world-cup",{"id":123,"doc_module":4,"doc_module_name":25,"category_name":124,"show_sort_weight":123,"slug":125},10,"Lifestyle","lifestyle",{"id":127,"doc_module":4,"doc_module_name":25,"category_name":128,"show_sort_weight":97,"slug":129},19,"General","general",{"code":4,"msg":82,"data":131},{"doc_id":79,"user_id":132,"nickname":42,"user_avatar":133,"doc_module":4,"category_id":102,"category_name":29,"doc_title":10,"doc_description":12,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":55,"is_deleted":4,"is_public":22,"is_downloadable":22,"audit_status":22,"page_count":110,"language":139,"language_code":8,"site_id":7,"html_lang":8,"table_of_contents":140,"faqs":141,"seo_title":142,"seo_description":12,"update_tm":80,"read_time":117,"preview_page_count":22,"preview_urls":143},962090896061,"https://ap-avatar.wpscdn.com/davatar_9964176cb1d06d4a9deccf72a44ae3dc","More Effective C\\#  \nSecond Edition  \nItem 14: Prefer Defining and Implementing Interfaces to Inheritance 􀁛 75  \nimplicitly enhanced. In that sense, base classes provide a way to extend the behavior of several types efficiently over time. When you add and implement functionality in the base class, all derived classes immediately incorporate that behavior. Adding a member to an interface, however, breaks all the classes that implement that interface. They will not contain the new method and will no longer compile. Each implementer must update that type to include the new member. Alternatively, if you need to add functionality to an interface without breaking the existing code, you can create a new interface and have it inherit from the existing interface.  \nChoosing between an abstract base class and an interface is a question of how best to support your abstractions over time. Interfaces are fixed:  \nYou release an interface as a contract for a set of functionality that any type can implement. In contrast, base classes can be extended overtime, and those extensions then become part of every derived class.  \nThe two models can be mixed to reuse implementation code while supporting multiple interfaces. One obvious example in the .NET Framework is the IEnumerable\u003CT> interface and the System .Linq.Enumerable class. The System.Linq.Enumerable class contains a large number of extension methods defined on the System .Collections .Generic .IEnumerable\u003CT> interface. That separation enables very important benefits. Any class that implements IEnumerable\u003CT> appears to include all those extension methods, but those additional methods are not formally defined in the IEnumerable\u003CT> interface. As a consequence, class developers do not need to create their own implementation of all those methods.  \nAs an example, consider the following class, which implements IEnumerable\u003CT> for weather observations:  \npublic enum Direction {  \nNorth,  \nNorthEast,  \nEast,  \nSouthEast,  \nSouth,  \nSouthWest,  \nWest,  \nNorthWest  \n}  \n76 􀁛 Chapter 2 API Design  \npublic class WeatherData  \n{  \npublic WeatherData(double temp, int speed,  \nDirection direction)  \n{  \nTemperature = temp;  \nWindSpeed = speed;  \nWindDirection = direction;  \n}  \npublic double Temperature { get; }  \npublic int WindSpeed { get; }  \npublic Direction WindDirection { get; }  \npublic override string ToString() =>  \n@$\"Temperature = {Temperature} , Wind is {WindSpeed} mph from the {WindDirection} \" ;  \n}  \npublic class WeatherDataStream : IEnumerable\u003CWeatherData>{  \nprivate Random generator = new Random();  \npublic WeatherDataStream(string location)  \n{  \n// Elided  \n}  \nprivate IEnumerator\u003CWeatherData> getElements(){  \n// Real implementation would read from  \n// a weather station .  \nfor (int i = 0; i \u003C 100; i++)  \nyield return new WeatherData (  \ntemp: generator.NextDouble() * 90,  \nspeed: generator.Next(70),  \ndirection: (Direction )generator.Next(7));  \n}  \npublic IEnumerator\u003CWeatherData> GetEnumerator() => getElements();  \nItem 14: Prefer Defining and Implementing Interfaces to Inheritance 􀁛 77 System.Collections .IEnumerator  \nSystem.Collections.IEnumerable.GetEnumerator() =>  \ngetElements();  \n}  \nTo model a sequence of weather observations, the WeatherStream class implements IEnumerable\u003CWeatherData>. That means creating two methods: the GetEnumerator\u003CT> method and the classic GetEnumerator method. The latter interface is explicitly implemented so that client code will naturally be drawn to the generic interface rather than the version typed as System.Object.  \nThe implementation of those two methods means that the WeatherStream class supports all the extension methods defined in System.Linq.Enumerable. That means WeatherStream can be a source for LINQ queries:  \nvar warmDays = from item in  \nnew WeatherDataStream ( \"Ann Arbor\" )  \nwhere item.Temperature > 80  \nselect item;  \nLINQ query syntax compiles to method calls. For example, the preceding query translates to the following calls:  \n","cbCaikdNvTvPLWtu","https://ap.wps.com/l/cbCaikdNvTvPLWtu","pdf",475973,"English","# Item 14: Prefer Defining and Implementing Interfaces to Inheritance\n## Interfaces as fixed contracts vs base classes\n## Mixing models for reuse (e.g., IEnumerable\u003CT> and extension methods)\n## Using interfaces for parameters and return values","[{\"question\":\"Why does adding members to an interface break implementing classes?\",\"answer\":\"Because every implementing type must update to include the new member; otherwise the new method is missing and the code no longer compiles.\"},{\"question\":\"How can you add functionality to an interface without breaking existing code?\",\"answer\":\"Create a new interface that inherits from the existing one, so existing implementations remain valid while new functionality is provided through the new contract.\"},{\"question\":\"What does implementing IEnumerable\\u003cT\\u003e enable for a type like WeatherDataStream?\",\"answer\":\"It enables LINQ extension methods defined for IEnumerable\\u003cT\\u003e, allowing the type to act as a source for LINQ queries.\"}]","More Effective C# - Second Edition - Item 14 - Prefer Defining and Implementing Interfaces to Inheritance | PDF",[144],{"page_no":22,"url":145,"width":146,"height":147},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-09/107e94863b6f9e7b1513772f04dd44f5.webp",983,1282,{"list":149,"token":11},[150,156,162,168,174,180,186,192,198,204],{"doc_id":151,"doc_title":152,"file_type":137,"home_preview_img_url":153,"slug":154,"view_count":55,"category_name":29,"create_tm":155},407983,"murach's - A Self-Paced Professional Book - Object-Oriented Book - Introduction to Database Programming","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229920863146/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111579&Signature=0L3O9a1rpBAMyB7l4Hy2BMewrtY%3D","murachs-a-self-paced-professional-book-object-oriented-book-introduction-to-database-programming",1790577509,{"doc_id":157,"doc_title":158,"file_type":137,"home_preview_img_url":159,"slug":160,"view_count":97,"category_name":29,"create_tm":161},705083,"The C# Player’s Guide - Second Edition - Learning C# programming basics and advanced topics","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230014616002/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111579&Signature=COaQBIcl1%2FSl2Jv46%2BR2WV13xw0%3D","the-c-players-guide-second-edition-learning-c-programming-basics-and-advanced-topics",1794682974,{"doc_id":163,"doc_title":164,"file_type":137,"home_preview_img_url":165,"slug":166,"view_count":102,"category_name":29,"create_tm":167},670910,"Fourth AP Edition - Object-Oriented Programming and Data Structures - Preface xiii","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229988353200/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111579&Signature=dMuxhgH7SQ%2Bd9o2KalE1MbqHHtM%3D","fourth-ap-edition-object-oriented-programming-and-data-structures-preface-xiii",1793572671,{"doc_id":169,"doc_title":170,"file_type":137,"home_preview_img_url":171,"slug":172,"view_count":102,"category_name":29,"create_tm":173},655757,"Library Management System - Java OOP Case Study","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229974776558/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111579&Signature=0wBEVoKjuvJ8m0ewHZuVIE3ThWE%3D","library-management-system-java-oop-case-study",1792963616,{"doc_id":175,"doc_title":176,"file_type":137,"home_preview_img_url":177,"slug":178,"view_count":110,"category_name":29,"create_tm":179},423010,"The Vocabulary of OOP - Slide 4","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229922611433/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111579&Signature=tBtlT7ZLAVD6AsxZVpic5Ge3%2BhY%3D","the-vocabulary-of-oop-slide-4",1790629742,{"doc_id":181,"doc_title":182,"file_type":137,"home_preview_img_url":183,"slug":184,"view_count":55,"category_name":29,"create_tm":185},655766,"CPS122 Lecture - Class Diagrams in UML","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229975289054/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111579&Signature=fl0Ypptcf%2BrRb9PtgwStRfJDHlU%3D","cps122-lecture-class-diagrams-in-uml",1792963653,{"doc_id":187,"doc_title":188,"file_type":137,"home_preview_img_url":189,"slug":190,"view_count":55,"category_name":29,"create_tm":191},694796,"UML - Modules vs. Classes","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230006941680/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111579&Signature=x2B0vBY2xHlkiWBBUYCk9LGC5vo%3D","uml-modules-vs-classes",1794347817,{"doc_id":193,"doc_title":194,"file_type":137,"home_preview_img_url":195,"slug":196,"view_count":30,"category_name":29,"create_tm":197},700986,"ASM 4.0 - Java bytecode engineering library - Version 2.0 September 2011","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230011575673/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111579&Signature=c2%2BSYlHWQ3vFf2k8UlSZrsgH7xo%3D","asm-40-java-bytecode-engineering-library-version-20-september-2011",1794522631,{"doc_id":199,"doc_title":200,"file_type":137,"home_preview_img_url":201,"slug":202,"view_count":123,"category_name":29,"create_tm":203},512859,"gWidgets2tcltk - Toolkit Implementation of gWidgets2 for tcltk - Maintainer John Verzani","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229936502558/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111579&Signature=unCqlUXCQBHOq%2B%2B3H7cTfX86QE8%3D","gwidgets2tcltk-toolkit-implementation-of-gwidgets2-for-tcltk-maintainer-john-verzani",1791276928,{"doc_id":205,"doc_title":206,"file_type":137,"home_preview_img_url":207,"slug":208,"view_count":97,"category_name":29,"create_tm":161},705082,"The C# Player's Guide - read online free","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230015594599/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111579&Signature=EE06NZJT7mK0sV3QYprJIPYeIA4%3D","the-c-players-guide-read-online-free"]