Skip to content

Tag Archives: inheritance

How to Add a Collection of a Derived Type/Subclass with Generic List AddRange()

20-Sep-09

Another gem today, when attempting to add a subclass collection to a base type list using the AddRange() method you may encounter the following error using this code: List<BaseClassA> baseList = new List<BaseClassA>(); baseList.AddRange((IEnumerable<SubClassB>) subList); // Where ‘SubClassB’ is a class deriving from ‘BaseClassA’ System.InvalidCastException was unhandled: Message=”Unable to cast object of type ‘System.Collections.Generic.List`1[MyNamespace.BaseClassA]‘ to [...]

My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...