Convert a List of BaseClass to a List of DerivedClass in C#
So there you are, you and your fantastic object-oriented design model. Abstracting away. Creating base classes and derived classes, and many tiers over each other. Now you start to deal with lists of these classes and you realize at some point you want to do some filtering. This situation comes up all the time for me, specifically when dealing with my derived classes. Here’s the scenario that I was in that required me to do some filtering and narrowing of my classes. I had a class diagram similar to something as basic as this:
BaseClass Derived1 : BaseClass Derived2 : BaseClass Derived3 : BaseClass
We’re talking basic object-oriented design here. I had a special black box of sorts (not going to get into it…) that always required me to pass it a List<BaseClass>. It would then know how to deal with all of the sub-types. This black box would also return me a List<BaseClass> and I’d need to sort out the contents myself. This isn’t exactly anything special and its called covariance and contravariance. Say you wanted to take a List<BaseClass> and only want to really get a List<Derived1> from it. There are some options here. You can loop over all the object in the given list and create another list of just Derived1, in each iteration checking if the current object is Dervied1 and adding it to the return list. That’s old school. Another option (which I prefer) is to use the OfType<T> filtering extension method in LINQ to filter the list on a specific type. This is quite simple to use:
var foo = new List<Color>(); foo.Add(new Red()); foo.Add(new Red()); foo.Add(new Blue()); foo.Add(new Green()); foo.Add(new Blue()); foo.Add(new Green()); foo.Add(new Red()); // let's get back only the Reds. *drum roll* foo.OfType<Red>(); // annnnd, done.
The OfType<T>() extension works by both narrowing a list from a BaseClass to a DerivedClass and widening it from a DerivedClass to a BaseClass as well:
var bar = new List<Red>(); bar.Add(new Red()); bar.Add(new Red()); bar.OfType<Color>(); // now its a List<Color> containing all Reds
More reading: Enumerable.OfType(Of TResult) Method
Got anything to say? Go ahead and leave a comment!
Popular Posts
- Using the DataSource Field with Sitecore Sublayouts
- Tame Your Sitecore Treelists
- Write to a Custom Sitecore Log with log4net
- Sitecore Upgrade Strategy
- Rendering Fully Qualified Sitecore URLs
- Sitecore Admin Pages Explained
- Managing CSS in the Sitecore Media Library
- Use Any() Instead of Count() To See if an IEnumerable Has Any Objects
- Sitecore Front-End Development Best Practices
- Scaling Sitecore Presentation Component Data Sources
Recent Comments
- Performance tuning your Sitecore installation | Agile and ALM: Software development today on A Going Live Checklist for Sitecore Websites
- Imran Saleem on Sitecore Avanced Database Crawler Occasionally Provides Null Results
- Ty Cahill on Sitecore Front-End Development Best Practices
- Sitecore Managed Sites as Virtual Folders | Fire Breaks Ice on Sitecore Item and Field Names
- Krimos on Using the DataSource Field with Sitecore Sublayouts
Sitecore Links
- .Sitecore
- Aboo Bolaky
- Alex Shyba
- Anders Dreyer
- aweber1.0
- Brian Pedersen
- Christopher Wojciech
- Coffee => Coder => Code
- Dev Sitecored²
- Everything Web
- Image0.com blog
- John West
- Learn Sitecore
- Let's do Sitecore
- Mark van Aalst
- Matthew Kenny
- Molten Core
- Project Lifecycle
- Sean Kearney
- Sebastian Patten
- Sitecore Australia
- Sitecore Blog
- Sitecore Climber
- Sitecore Development
- Sitecore Gadgets
- Techphoria414
- The Client View
- The Sitecore Experience
- Web Content Management and Delivery
Archives
- April 2013 (1)
- February 2013 (1)
- January 2013 (1)
- December 2012 (1)
- June 2012 (2)
- May 2012 (2)
- March 2012 (1)
- February 2012 (1)
- January 2012 (5)
- December 2011 (4)
- November 2011 (1)
- July 2011 (1)
- June 2011 (1)
- May 2011 (2)
- March 2011 (6)
- February 2011 (2)
- January 2011 (10)

Posted under: