Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 not getting access to LINQ IEnumerable 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
 
class ComparingClass : IComparer<Class1>
 
to
 
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 22-Apr-11 1:31am
Lee Reid1.6K
Edited 22-Apr-11 1:49am
v4
Comments
Nishant Sivakumar at 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 at 22-Apr-11 7:39am
   
Done. Sorry about that!
Nishant Sivakumar at 22-Apr-11 7:47am
   
Thanks, I've replied to you.
   
This is the first time I've seen anyone use the where clause.
Nishant Sivakumar at 22-Apr-11 7:47am
   
Wow, really? I use it all the time :-)
Lee Reid at 22-Apr-11 7:53am
   
whoa, seriously?? I'm not sure how i'd survive without it
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

That's expected behavior. .NET 4.0's version of IComparer<> is contravariant, see below:
 
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:
 
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[^]
  Permalink  
v2
Comments
Lee Reid at 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
Nishant Sivakumar at 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.
SAKryukov at 22-Apr-11 22:45pm
   
Great answer! My 5.
--SA
Nishant Sivakumar at 23-Apr-11 9:18am
   
Thank you, SA.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
Comments
Nishant Sivakumar at 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 at 22-Apr-11 7:54am
   
It looked very plausible but you are correct. Have my 5 :-)
Nishant Sivakumar at 22-Apr-11 7:56am
   
Thank you.
Lee Reid at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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!
  Permalink  

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

  Print Answers RSS
0 CHill60 360
1 OriginalGriff 303
2 Sarvesh Kumar Gupta 168
3 RyanDev 145
4 Sergey Alexandrovich Kryukov 128


Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 22 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid