« Variance » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications
Ligne 3 : Ligne 3 :
* [[List#Covariance|List]]
* [[List#Covariance|List]]
* [[CSharp_4#Covariance_et_Contra_variance_des_g.C3.A9n.C3.A9riques|Generics]]
* [[CSharp_4#Covariance_et_Contra_variance_des_g.C3.A9n.C3.A9riques|Generics]]
* [https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance/ Covariance and Contravariance]
 
= [https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance Description] =
Covariance enable implicit reference conversion for array types, delegate types, and generic type arguments.<br>
Covariance preserves assignment compatibility and contravariance reverses it.
 
= Assignment compatibility =
<kode lang='cs'>
string s = "test"; 
 
// An object of a more derived type is assigned to an object of a less derived type.
object o = s;
</kode>

Version du 26 mars 2024 à 14:15

Links

Description

Covariance enable implicit reference conversion for array types, delegate types, and generic type arguments.
Covariance preserves assignment compatibility and contravariance reverses it.

Assignment compatibility

Cs.svg
string s = "test";  

// An object of a more derived type is assigned to an object of a less derived type.
object o = s;