|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Polymorphic substitutability not supported it seems?When I try to compile this class, I get two compilation errors: Error 1 The best overloaded method match for 'PolymorphismTest.PolymorphismTest.DoSomethingElse(System.Collections.Generic.IList<object>)' has some invalid arguments C:\Documents and Settings\aw\My Documents\Visual Studio 2005\Projects\Reporting\PolymorphismTest.cs 16 13 ReportsGenerator Error 2 Argument '1': cannot convert from 'System.Collections.Generic.IList<PolymorphismTest.Special>' to 'System.Collections.Generic.IList<object>' C:\Documents and Settings\aw\My Documents\Visual Studio 2005\Projects\Reporting\PolymorphismTest.cs 16 29 ReportsGenerator using System; using System.Collections.Generic; namespace PolymorphismTest { public class PolymorphismTest { public void DoSomethingElse(IList<Object> objects) { } public void DoSomething() { IList<Special> specialObjects = new List<Special>(); DoSomethingElse(specialObjects); } } public class Special : Object { } } As far as I can see this should work - I must be missing something? can anyone help? Thanks in advance :) -- It is the unknown that defines our existence skywalker <skywal***@discussions.microsoft.com> wrote:
<snip> > As far as I can see this should work - I must be missing something? Yes. IList<SomeType> isn't convertible to IList<Object>. Generic types don't have covariance. The reason for this is simple. Consider if DoSomethingElse did: objects.Add (new object()); You've just passed in an IList<Special>... what should happen? A runtime exception? One of the points of generics is to give compile- time type safety, avoiding runtime exceptions due to type issues. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Other interesting topics
|
|||||||||||||||||||||||