Friday, December 11, 2015

C# Delegate with return value

I have written another simple Delegate code to share with students in "20483 Programming in C#", this code showing the delegate with parameters and return value and how we supply the parameters and receive the return value.
 
 
using System;

namespace EventDelegatePOC
{
   delegate int MyDelegate(string s);

   class Program
   {
      static int SayHello(string HelloMessage)
      {
          Console.WriteLine("Hello {0}", HelloMessage);
          return 100;
      }
      static int SayGoodBye(string GoodbyeMessage)
      {
          Console.WriteLine("Good Bye {0}", GoodbyeMessage);
          return -999;
      }
      static void Main(string[] args)
      {
          MyDelegate x = new MyDelegate(SayHello);
          MyDelegate y = new MyDelegate(SayGoodBye);

          int a = x("President");
          int b = y("Sayonara");

          Console.WriteLine("a = {0}", a);
          Console.WriteLine("b = {0}", b);
      }
   }
}
Here is the ouput:-

No comments: