« Variance » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 21 : Ligne 21 :
{{info | It works only with covariant interface}}
{{info | It works only with covariant interface}}
<kode lang='cs'>
<kode lang='cs'>
var strings = new List<string>();
var deriveds = new List<Derived>();
List<object> objects = strings;  // Cannot convert type List<string> to List<object> because List<T> is invariant
List<Base> bases = strings;  // Cannot convert type List<string> to List<object> because List<T> is invariant
IList<object> objects = strings;  // Cannot implicitly convert type List<string> to IList<object> because IList<T> is invariant
IList<Base> bases = strings;  // Cannot implicitly convert type List<string> to IList<object> because IList<T> is invariant


IEnumerable<object> objects = strings; // IEnumerable<T> is covariant
IEnumerable<object> bases = deriveds;       // IEnumerable<T> is covariant
IReadOnlyList<Base> bases = deriveds;      // IReadOnlyList<T> is covariant
IReadOnlyCollection<Base> bases = deriveds; // IReadOnlyCollection<T> is covariant


var objects = (IList<object>)strings; // ???
var objects = (IList<object>)strings; // ???

Version du 26 mars 2024 à 17:08

Links

Description

Variance allows to replace a type with a less-derived (covariance: derived → base) type or a more-derived type (contravariance: base → derived).
Covariance enable implicit reference conversion for array types, delegate types, and generic type arguments.
Covariance preserves assignment compatibility and contravariance reverses it.

Since .NET Framework 4, C# supports covariance and contravariance in generic interfaces and delegates and allows for implicit conversion of generic type parameters.

Assignment compatibility

Allow an object of a more derived type (child class, ex: string) to be assigned to an object of a less derived type (parent class, ex: object).

Cs.svg
string s = "test";
object o = s;

Covariance

Allow a generic object of child class type (ex: string) to be assigned to a generic object of parent class type (ex: object).

It works only with covariant interface
Cs.svg
var deriveds = new List<Derived>();
List<Base> bases = strings;   // Cannot convert type List<string> to List<object> because List<T> is invariant
IList<Base> bases = strings;  // Cannot implicitly convert type List<string> to IList<object> because IList<T> is invariant

IEnumerable<object> bases = deriveds;       // IEnumerable<T> is covariant
IReadOnlyList<Base> bases = deriveds;       // IReadOnlyList<T> is covariant
IReadOnlyCollection<Base> bases = deriveds; // IReadOnlyCollection<T> is covariant

var objects = (IList<object>)strings; // ???

Native covariant interfaces

Interface Since
IEnumerable<out T> .NET Framework 4.0
IReadOnlyList<out T> .NET Framework 4.5
IReadOnlyCollection<out T> .NET Framework 4.5