Covarianza finalmente nel C# 4.0

Seguendo il web cast “Raf & Alk su C# 4” di dotnetmarche ho scoperto l’introduzione di questa feature nel framework 4.0 con cui mi ero scontrato tempo fa e che mi sembrava quasi scontata.

Dalla msdn http://msdn.microsoft.com/it-it/library/ee207183.aspx

// Assignment compatibility. 
string str = "test";
// An object of a more derived type is assigned to an object of a less derived type. 
object obj = str;

// Covariance. 
IEnumerable<string> strings = new List<string>();
// An object that is instantiated with a more derived type argument 
// is assigned to an object instantiated with a less derived type argument. 
// Assignment compatibility is preserved. 
IEnumerable<object> objects = strings;

// Contravariance.           
// Assume that the following method is in the class: 
// static void SetObject(object o) { } 
Action<object> actObject = SetObject;
// An object that is instantiated with a less derived type argument 
// is assigned to an object instantiated with a more derived type argument. 
// Assignment compatibility is reversed. 
Action<string> actString = actObject;
Tag di Technorati: ,

posted @ giovedì 24 marzo 2011 14:43

Print
Comments have been closed on this topic.
«luglio»
domlunmarmergiovensab
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910