Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hi everyone,
I'm in the process of putting a massive amount of c# code originally written for .NET 4.0 into a silverlight 4 app. I have quite a serious problem with the compiler rejecting generics when inheritance is involved. I've put a very basic example down below: this code compiles OK in .NET 4, but not in silverlight 4.0 or .NET 3.5. Can someone with more experience please tell me what's going on here?? I surely can't be the first to come across such issues:
using System;
using System.Collections.Generic;

namespace ClassLibrary2
{
    public class Main<T>
        where T : Class1
    {
        public void DoStuff()
        {
            //The following line compiles in .NET4 but does not compile in Silverlight 4
            //Error	2	Argument 1: cannot convert from 'ClassLibrary2.ComparingClass' to 'System.Collections.Generic.IComparer<T>'
            Class2<T, IComparer<T>> returnVal = new Class2<T, IComparer<T>>(new ComparingClass());
        }
    }

    public class Class1
    {
    }

    class Class2<T, U>
        where T : Class1
        where U : IComparer<T>
    {
        internal Class2(U c)
        {
        }
    }

    class ComparingClass : IComparer<Class1>
    {
        public int Compare(Class1 a, Class1 b)
        {
            return 1;
        }
    }
}


I have similar problems with things inheriting from List<t> not getting access to LINQ IEnumerable<t> extension methods etc. All of this code compiled A-OK in .NET 4...
Any help will be greatly appreciated.

EDIT:

I've found a solution *to this example*:
change

XML
class ComparingClass : IComparer<Class1>


to

VB
class ComparingClass<T> : IComparer<T>
        where T:Class1


but in general I don't understand why the compiler thinks of them differently. I have a lot of code to move across with similar issues and need to understand what's going wrong in order to fix it. Help!
Posted
Updated 22-Apr-11 1:49am
v4
Comments
Nish Nishant 22-Apr-11 7:36am    
The formatting is messed up, some of the code is swallowed as it gets treated as html tags. Can you fix that?
Lee Reid 22-Apr-11 7:39am    
Done. Sorry about that!
Nish Nishant 22-Apr-11 7:47am    
Thanks, I've replied to you.
#realJSOP 22-Apr-11 7:42am    
This is the first time I've seen anyone use the where clause.
Nish Nishant 22-Apr-11 7:47am    
Wow, really? I use it all the time :-)

That's expected behavior. .NET 4.0's version of IComparer<> is contravariant, see below:

C#
public interface IComparer<in T>


In .NET 3.5 there was no notion of contravariance. So in 3.5 you would need a cast as follows:

C#
Class2<T, IComparer<T>> returnVal = new Class2<T, IComparer<T>>(
      (IComparer<T>)new ComparingClass());


For more info, see a blog entry I did some time ago:

C# 4.0 and variant generic interfaces[^]
 
Share this answer
 
v2
Comments
Lee Reid 22-Apr-11 7:54am    
No contravariance?? Oh lord do I have a lot of work ahead of me then :| Thanks heaps for your reply
Nish Nishant 22-Apr-11 7:55am    
You are welcome. Yeah moving back to 3.5 was not a very pleasant experience for me either when I had to do that last year for a personal project. A lot of my LINQ stuff broke because of this variance issue.
Sergey Alexandrovich Kryukov 22-Apr-11 22:45pm    
Great answer! My 5.
--SA
Nish Nishant 23-Apr-11 9:18am    
Thank you, SA.
Looks like IComparer was introduced in version 4 and therefor not available in earlier versions. Have a look here:
http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx[^]

Good luck!
 
Share this answer
 
Comments
Nish Nishant 22-Apr-11 7:48am    
>> Looks like IComparer was introduced in version 4 and therefor not available in earlier versions. << .......... Not really. Please see my answer.
E.F. Nijboer 22-Apr-11 7:54am    
It looked very plausible but you are correct. Have my 5 :-)
Nish Nishant 22-Apr-11 7:56am    
Thank you.
Lee Reid 22-Apr-11 7:50am    
Thanks for your help, but it appears its been there for some time:
"Supported in: 4, 3.5, 3.0, 2.0"
E.F. Nijboer 22-Apr-11 7:56am    
Yes, it was also mentioned by Nishant Sivakuma. Luckily he gave you a better answer that is hopefully very helpful to you.
Update:
In case anybody else is having similar issues switching to Silverlight from .NET 4:
I've just downloaded Silverlight 5 Beta and found that these 'problems' do not exist in that framework. Upgrade!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900