Delegate Return type compatibility - covariance - CSharp Custom Type

CSharp examples for Custom Type:delegate

Introduction

A delegate's target method may return a more specific type than described by the delegate. This is called covariance.

delegate return types are covariant.

Demo Code

using System;// w  w w . j  av  a2 s.c om
delegate object ObjectRetriever();
class Test
{
   static void Main()
   {
      ObjectRetriever o = new ObjectRetriever (RetrieveString);
      object result = o();
      Console.WriteLine (result);      // hello
   }
   static string RetrieveString() => "hello";
}

Result


Related Tutorials