I read recently about covariance and contravariance - little code example below:
public class BaseClass{
public int x = 1;
public static void print(BaseClass objClass)
{
Console.WriteLine(objClass.GetType().Name + " " + objClass.x);
}
}
public class DerivedClass : BaseClass{
public int x = 2;
}
public class Program
{
public static void Main(string[] args)
{
BaseClass bC = new BaseClass();
DerivedClass dC = new DerivedClass();
BaseClass.print(bC); //DerivedClass 1
DerivedClass.print(bC); //DerivedClass 1
BaseClass.print(dC); //DerivedClass 1
DerivedClass.print(dC); //DerivedClass 1
}
}
My question is - what contravariance give us in practice ? I know that I can pass object of DerivedClass as argument into BaseClass method (where parameter is BaseClass type) but what are the benefits of such operation ? And why when I pass DerivedClass object it returns me value of x in BaseClass ?
Maybe this is bad example for explain benefits of contravariance then I would be grateful for another example.