I need to understand the difference between Covariance & Contravariance with a simple example as there are many articles which only talke about covariance but not about contravariance.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Francis SusaimichaelRank #466 · 5.4K reputationPosted Oct 22, 2015, 11:30 PM EDT
Normally, the method that you pass to a delegate must have the same return type and signature as the delegate. However, covariance and contravariance relax this rule slightly, as it pertains to derived types. Covariance enables a method to be assigned to a delegate when the method’s return type is a class derived from the class specified by the return type of the delegate. Contravariance enables a method to be assigned to a delegate when a method’s parameter type is a base class of the class specified by the delegate’s declaration.
Francis SusaimichaelRank #466 · 5.4K reputationPosted Oct 23, 2015, 1:30 AM EDT
Ravi PatelRank #439 · 5.7K reputationPosted Oct 22, 2015, 10:57 PM EDT
Sumit AroraRank #1,926 · 762 reputationPosted Oct 22, 2015, 11:51 PM EDT
Rajesh SinghRank #1,806 · 824 reputationPosted Dec 26, 2015, 10:15 PM EST