0

Can you give practical usage of using generics-Contravariance (will be good if from both infrastructure and custom example).

Thanks.

roman
  • 23
  • 4
  • 3
    Possible duplicate of [Covariance and contravariance real world example](http://stackoverflow.com/questions/2662369/covariance-and-contravariance-real-world-example) – Pawel Gradecki Apr 19 '17 at 10:57

1 Answers1

0

Microsoft's documentation use to be a very good place to find this kind of examples:

https://msdn.microsoft.com/en-us/library/dd799517(v=vs.110).aspx

using System;
using System.Collections.Generic;

abstract class Shape
{
    public virtual double Area { get { return 0; }}
}

class Circle : Shape
{
    private double r;
    public Circle(double radius) { r = radius; }
    public double Radius { get { return r; }}
    public override double Area { get { return Math.PI * r * r; }}
}

class ShapeAreaComparer : System.Collections.Generic.IComparer<Shape>
{
    int IComparer<Shape>.Compare(Shape a, Shape b) 
    { 
        if (a == null) return b == null ? 0 : -1;
        return b == null ? 1 : a.Area.CompareTo(b.Area);
    }
}

class Program
{
    static void Main()
    {
        // You can pass ShapeAreaComparer, which implements IComparer<Shape>,
        // even though the constructor for SortedSet<Circle> expects 
        // IComparer<Circle>, because type parameter T of IComparer<T> is
        // contravariant.
        SortedSet<Circle> circlesByArea = 
            new SortedSet<Circle>(new ShapeAreaComparer()) 
                { new Circle(7.2), new Circle(100), null, new Circle(.01) };

        foreach (Circle c in circlesByArea)
        {
            Console.WriteLine(c == null ? "null" : "Circle with area " + c.Area);
        }
    }
}

/* This code example produces the following output:

null
Circle with area 0.000314159265358979
Circle with area 162.860163162095
Circle with area 31415.9265358979
 */
brduca
  • 3,573
  • 2
  • 22
  • 30