compiled time support for Covariant and Contravariant in .net 2.0
In .net 2.0, A delegate can be associated with a named method, this facility provide support for covariant and contravariant as the following sample code shows.
delegate void ProcessString(string item);
delegate object GetObject();
void RunObject(object item)
{
Console.WriteLine(item);
}
string RetrieveString()
{
return string.Empty;
}
[TestMethod]
public void delegate_shortcut_suport_contraVariant()
{
//delegate with derived type parameter input <--(accept) method with base type parameter input
ProcessString processString = this.RunObject;
processString("string");
//-->
RunObject("string");
}
[TestMethod]
public void delegate_shortcut_support_coVariant()
{
//delegate with base type output <--(accept) method with derived type output
GetObject getObject = this.RetrieveString;
object returnValue = getObject();
//-->
object returnValue2 = this.RetrieveString();
}