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:

[csharp]
BaseClass
Derived1 : BaseClass
Derived2 : BaseClass
Derived3 : BaseClass
[/csharp]

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:

[csharp]
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.
[/csharp]

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:

[csharp]
var bar = new List<Red>();
bar.Add(new Red());
bar.Add(new Red());

bar.OfType<Color>(); // now its a List<Color> containing all Reds
[/csharp]

More reading: Enumerable.OfType(Of TResult) Method

 

Mark Ursino

Mark is Sr. Director at Rightpoint and a Sitecore MVP.

 

One thought on “Convert a List of BaseClass to a List of DerivedClass in C#

  1. Hi,
    Very precise post. But IMHO the headline is a bit off. You are not converting the list back and forth – OfType is just filtering the lists. Found out the hard way. But hey … my bad – I could just have read your post instead of just scrolling down to the conclusion 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.