O'Reilly logo

Visual C#® 2012: How to Program, Fifth Edition by Harvey Deitel, Paul Deitel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

21.6. Covariance and Contravariance for Generic Types

C# supports covariance and contravariance of generic interface and delegate types. We’ll consider these concepts in the context of arrays, which have always been covariant and contravariant in C#.

Covariance in Arrays

Recall our Employee class hierarchy from Section 12.5, which consisted of the base class Employee and the derived classes SalariedEmployee, CommissionEmployee and BasePlusCommissionEmployee. Assuming the declarations

SalariedEmployee[] salariedEmployees = {    new SalariedEmployee( "Bob", "Blue", "111-11-1111", 800M ),    new SalariedEmployee( "Rachel", "Red", "222-22-2222", 1234M ) }; Employee[] employees;

we can write the following statement: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required