Generic variance is the ability to assign a generic interface or delegate type to the same type with another parameter, for example, assign IEnumerable
Questions tagged [generic-variance]
58 questions
109
votes
2 answers
How is Generic Covariance & Contra-variance Implemented in C# 4.0?
I didn't attend PDC 2008, but I heard some news that C# 4.0 is announced to support Generic covariance and contra-variance. That is, List can be assigned to List

Morgan Cheng
- 73,950
- 66
- 171
- 230
20
votes
2 answers
Why doesn't C# support variant generic classes?
Take this small LINQPad example:
void Main()
{
Foo

Kendall Frey
- 43,130
- 20
- 110
- 148
19
votes
3 answers
Customizing Autofac's component resolution / Issue with generic co-/contravariance
First, sorry for the vague question title. I couldn't come up with a more precise one.
Given these types:
{ TCommand : ICommand }
«interface» «interface» /
…

stakx - no longer contributing
- 83,039
- 20
- 168
- 268
16
votes
3 answers
Generic Variance in C# 4.0
Generic Variance in C# 4.0 has been implemented in such a way that it's possible to write the following without an exception (which is what would happen in C# 3.0):
List intList = new List();
List

Daniel May
- 8,156
- 1
- 33
- 43
11
votes
1 answer
How to determine type parameter's variance?
Inspired by Real-world examples of co- and contravariance in Scala I thought a better question would be:
When designing a library, are there a specific set of questions you should ask yourself when determining whether a type parameter should be…

Bradford
- 4,143
- 2
- 34
- 44
11
votes
3 answers
Why does the variance of a class type parameter have to match the variance of its methods' return/argument type parameters?
The following raises complaints:
interface IInvariant {}
interface ICovariant {
IInvariant M(); // The covariant type parameter `TCov'
// must be invariantly valid on
//…

concat
- 3,107
- 16
- 30
11
votes
3 answers
Co/contravariance with Func as parameter
Assume I have an interface such as
public interface IInterface {
IInterface DoSomething(TIn input);
}
TIn being contra-variant, and TOut being co-variant.
Now, I want callers to be able to specify some function to be…

knittl
- 246,190
- 53
- 318
- 364
7
votes
2 answers
Why kotlin doesn't allow covariant mutablemap to be a delegate?
I'm new to Kotlin.
When I learn Storing Properties in a Map. I try following usage.
class User(val map: MutableMap) {
val name: String by map
}
class User(val map: MutableMap) {
val name: String by…

Dean Xu
- 4,438
- 1
- 17
- 44
6
votes
1 answer
Subtyping between function types
In coursera functional programming course, I came across a subtle concept.
If A2 <: A1 and B1 <: B2, then (A1 => B1) <: (A2 => B2)
Justification
when we pass an argument to A2 and because of the subtyping relationship, we can pass the same argument…

tharindu_DG
- 8,900
- 6
- 52
- 64
6
votes
2 answers
Can C# 4.0 variance help me call a base class constructor with an upcast?
I was reading a bit on generic variance and I don't have a full understanding of it yet but I'd like to know if it makes something like the following possible?
class A { }
class B { }
class C : B { }
class My1 {
public My1(A…

Aaron Anodide
- 16,906
- 15
- 62
- 121
5
votes
2 answers
Why can I cast the invariance of IList away?
Currently I'm preparing a presentation of the new generic variance features in C# for my colleagues. To cut the story short I wrote following lines:
IList

Nico
- 1,554
- 1
- 23
- 35
5
votes
2 answers
Circumventing variance checks with extension methods
This doesn't compile:
class MyClass[+A] {
def myMethod(a: A): A = a
}
//error: covariant type A occurs in contravariant position in type A of value a
Alright, fair enough. But this does compile:
class MyClass[+A]
implicit class…

Lasf
- 2,536
- 1
- 16
- 35
5
votes
2 answers
Why do lower type bounds change the variance position?
The Scala Language Specification (Section 4.5 on Variance Annotations, p. 44) says
The variance position of a type parameter is the opposite of the variance position
of the enclosing type parameter clause.
The variance position of the lower bound…

godfatherofpolka
- 1,645
- 1
- 11
- 24
5
votes
2 answers
Single extension method on IDictionary>
I'm trying to write an extension method that will convert IDictionary> holding any type of collection/sequence (S) to ILookup which is more proper data structure in those cases. This means I'd like my extension to work on different…

NOtherDev
- 9,542
- 2
- 36
- 47
4
votes
2 answers
Multiple Generics ambiguity
The codes below are exactly the same, except that one is C# and the other one is VB.Net.
C# compiles just fine, but VB.Net throws the warning:
Interface 'System.IObserver(Of Foo)' is ambiguous with another
implemented interface…

Marcelo de Aguiar
- 1,432
- 12
- 31