Covariance – intro

  • Covariance is implemented with generics.
  • Out keyword has to be used
    • this will not work !!
      IPlay<Cricket> cricketer= new Play<ODICricket>();
				
					namespace Covariance
{
    public interface IPlay<T>
    {
        void Play();
    }

    public class Play<T> : IPlay<T>
    {
        void IPlay<T>.Play()
        {
            Console.WriteLine("Play Cricket!!");
        }
    }

    public class Cricket
    {
        public virtual void Play()
        {
        }
    }

    public class ODICricket : Cricket
    {
        public override void Play()
        {
        }
    }

    public class T20Cricket : Cricket
    {
        public override void Play()
        {
        }
    }
}

				
			
				
					using Covariance;

//this will not work !!
IPlay<Cricket> cricketer= new Play<ODICricket>();
				
			

Leave a Comment