0

I am trying to understand the contravariance in generic delegates. In the following code I am able to assign the delegate sgd1 to sgd2(covariance). Even if the type parameter of sgd4 is base type of the type parameter of sgd3 why am I not able to assign sgd3 to sgd4(contravariance).

 class Program
        {
            public delegate R SampleGenericDelegate<in A, out R>(A a);  
            static void Main(string[] args)
            {
                SampleGenericDelegate<string, string> sgd1= TakesObjectReturnsString;   
                SampleGenericDelegate<string, object> sgd2= sgd1;    //Delegate with more base type return type
                SampleGenericDelegate<string, string> sgd3= TakesObjectReturnsString;    
                SampleGenericDelegate<object, string> sgd4= sgd3;   //Delegate with more base type type parameter   //throwing error                 
            }
            public static string TakesObjectReturnsString(object s)
            {
                return "";
            }
        }
ravikiran
  • 33
  • 6

0 Answers0