I have an interface:
public interface IMyInterface { }
I have a class which implements the interface:
public class MyClass : IMyInterface { }
I have a function which asks for an IEnumerable of IMyInterface:
private void TestFunction(IEnumerable<IMyInterface> collectie) { }
I can't figure out why this doesn't work:
ObservableCollection<MyClass> myClasses = new ObservableCollection<MyClass>();
TestFunction(myClasses); // this line doesn't work
An ObservableCollection is an IEnumerable, right?
And my class does implement, right?
So why doesn't this work?
It gives this 2 errors:
Error 1 The best overloaded method match for 'WindowsFormsApplication1.Form1.TestFunction(System.Collections.Generic.IEnumerable)' has some invalid arguments C:\spike\spike_rtf\WindowsFormsApplication1\Form1.cs 59 4 WindowsFormsApplication1 Error 2 Argument '1': cannot convert from 'System.Collections.ObjectModel.ObservableCollection' to 'System.Collections.Generic.IEnumerable' C:\spike\spike_rtf\WindowsFormsApplication1\Form1.cs 59 17 WindowsFormsApplication1
You're after generic variance. This will be supported in .NET 4.0 and C# 4.0, only for interfaces and delegates (and only where appropriate). That's okay for your case though as you're interested in the IEnumerable<T> interface which becomes IEnumerable<out T> in .NET to indicate its covariance.
The simplest way to do this in .NET 3.5 is probably to use Cast<>:
TestFunction(myClasses.Cast<IMyInterface>());
(This is like User Friendly's "Select" suggestion, but a bit simpler. It's also worth knowing about for cases where it wouldn't work in C# 4 - if you happen to know that every item in a collection will be of the right type, but you can't express that in the type system.)
To find out (a lot) more about this, see Eric Lippert's blog posts on the topic. Be prepared for your mind to melt - I know mine does when I think about variance too hard. Eric has written various answers on this topic here on Stack Overflow too, including this one suggested in the comments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With