Delegate Parameter compatibility - contravariance - CSharp Custom Type

CSharp examples for Custom Type:delegate

Introduction

A delegate can have more specific parameter types than its method target. This is called contravariance.

As with type parameter variance, delegates are variant only for reference conversions.

Demo Code

using System;/*w w w. j  av  a  2s .com*/
delegate void StringAction (string s);
class Test
{
   static void Main()
   {
      StringAction sa = new StringAction (ActOnObject);
      sa ("hello");
   }
   static void ActOnObject (object o) => Console.WriteLine (o);   // hello
}

Result


Related Tutorials