Covariance example

Key points

					using System;
using System.Collections.Generic;

namespace Covariance
    class Fruit { }
    class Orange : Fruit { }
    class Apple : Fruit { }

    class Program
        static void Main(string[] args)
            //this works fine
            Fruit obj = new Apple();

            //I am allowed to add orange to bunch of apples...
            //so below code will not work....
            List<Fruit> appleObjects = new List<Apple>();
            appleObjects.Add(new Orange());

            //Make it Ienumerable...This will work..
            IEnumerable<Fruit> appleObjects1 = new List<Apple>();
             //appleObjects1.Add(new Orange());



Leave a Comment