Wow - it's a brave new world coming.
Using convariance does indeed return IHandle<BuildFailed> & IHandle<BuildComplete> instances. Taking out covariance only gives back the IHandle<BuildFailed> instances. Just when I thought I had co/contra-variance worked out! Sheesh. James. From: [email protected] [mailto:[email protected]] On Behalf Of Michael Minutillo Sent: Thursday, 25 March 2010 17:54 To: ozDotNet Subject: C# 4.0 Covariance Question I don't have VS2010 with me at the moment but I was wondering, in C# 4.0 Given: class Message { } class BuildComplete : Message { } interface IHandle<T> where T : Message { } I know in C# 3.0 that if I have a collection of objects and some of them implement the handler interface I can use the following line to give me a strongly typed collection of handlers IList<object> collection = ... IList<IHandle<BuildComplete>> handlers = collection.OfType<IHandle<BuildComplete>>().ToList(); Now, with the co/contra crazy going on in C# 4.0 (I can never remember which is supposed to be which) if I add a couple of new message classes: class BuildFailed : BuildComplete { } class BuildSucceeded : BuildComplete { } will the following line get me handlers of JUST BuildFailed or will handlers of BuildComplete be in my collection as well? IList<IHandle<BuildFailed>> handlers = collection.OfType<IHandle<BuildFailed>>().ToList(); I'm guessing that it will but only if I adjust my handler interface to be covariant interface IHandle<in T> where T : Message { } Can someone with VS2010 confirm? -- Michael M. Minutillo Indiscriminate Information Sponge Blog: http://wolfbyte-net.blogspot.com
