Monday, May 25, 2009

Delegate Covariance and Contra-variance in C# 4.0

C# 4.0 introduces co and contra-variance. Consider this code:



The out and in keywords in the delegate definitions above specify the delegates to be co or contra variant. out keyword says that, Child class of T can be used instead of T in Func1 definition. While in keyword specifies that the base class of T can be used instead of T in Action1 definition.
Try to play with this code, and try switching the places of out and in keywords in the above delegate definitions.

No comments:

Post a Comment