Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get the following error? Invalid variance modifier. Only interface and delegate type parameters can be specified as variant

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

namespace Variance

{
  class A { }

  class B : A { }

  class C<out T>  { }

class Program
{
    static void Main(string[] args)
    {
        var v = new C<B>();

        CA(v);
    }

    static void CA(C<A> v) { }
  }
}
like image 246
Franck Jeannin Avatar asked Jan 11 '10 15:01

Franck Jeannin


People also ask

Is there a variant modifier for invalid variance?

Invalid variance modifier. Only interface and delegate type parameters can be specified as variant Ask Question Asked11 years, 11 months ago Active4 years, 4 months ago Viewed5k times 10

What is invalid variance in icarmanager?

Invalid variance: The type parameter ‘T’ must be covariantly valid on ‘IBuildingManager<T>.Sell ()’. ‘T’ is contravariant. Covariance (<out T>) – Example! Again, we have an interface with specified variance – this time covariance. Which of course means, type T can only be returned by ICarManager ’s methods.

Can interface and delegate type parameters be specified as variant?

Only interface and delegate type parameters can be specified as variant Ask Question Asked11 years, 11 months ago Active4 years, 4 months ago Viewed5k times 10

Why am I getting an invalid variant operation error?

After you update, you may get this error: "Invalid variant operation". To fix this you may need to force the next update. Occasionally after an Acclipse Document Manager update is released some clients have report they start having issues with filing, closing emails and so on.


1 Answers

This is the offending line:

class C<out T> 

As the error message tells you, you can't apply generic variance to classes, only to interfaces and delegates. This would be okay:

interface C<out T>

The above is not.

For details, see Creating Variant Generic Interfaces

like image 58
jason Avatar answered Dec 09 '22 23:12

jason