when is generic covariance/contravariance is useful/necessary?

This is a discussion on when is generic covariance/contravariance is useful/necessary? within the C# Programming forums, part of the General Programming Boards category; Generic covariance and contravariance allow you to declare a generic interface or generic delegate whose variables can be initialized using ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    when is generic covariance/contravariance is useful/necessary?

    Generic covariance and contravariance allow you to declare a generic interface or generic delegate whose variables can be initialized using expressions whose type argument can be more/less derived than the type argument of the variable.

    In other words, without covariance and contravariance we can do this:

    Code:
            delegate void D<T>();
            interface I<T> { };
            class C<T> : I<T> { };
    
            static void Main(string[] args)
            {
                I<StringBuilder> i = new C<StringBuilder>();
                D<StringBuilder> d = new D<StringBuilder>(() => { });
    
                Console.Read();
            }
    But with generic covariance/contravariance we can also do this:

    Code:
            delegate void D<in T>();
            interface I<out T> { };
            class C<T> : I<T> { };
    
            static void Main(string[] args)
            {
                I<object> i = new C<StringBuilder>();
                D<StringBuilder> d = new D<object>(() => { });
    
                Console.Read();
            }
    That said, can someone help me think of an example where generic covariance/contravariance is necessary or at least useful?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    How about when you don't know what you're dealing with?

    Code:
    public interface IBird { void Fly(); }
    public class Pigeon : IBird { }
    public class Raven : IBird { }
    
    public Foo()
    {
      List<Pigeon> pigeons = new List<Pigeon>();
      Bar(pigeons);
    }
    
    public Bar(IEnumerable<IBird> birds)
    {
      foreach(IBird bird in birds)
        bird.Fly();
    }
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. efficient covariance matrix
    By qualia in forum C Programming
    Replies: 0
    Last Post: 08-26-2010, 06:11 PM
  2. Generic pointer
    By neu_greg in forum C Programming
    Replies: 3
    Last Post: 04-02-2009, 02:30 PM
  3. Covariance with virtual functions and stl containers
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2009, 07:26 AM
  4. Generic USB controllers
    By zacs7 in forum Tech Board
    Replies: 3
    Last Post: 12-09-2007, 12:48 AM
  5. help with generic function
    By joeyzt in forum C++ Programming
    Replies: 16
    Last Post: 01-05-2006, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21