Delegates

Delegates are the mechanism for turning methods or fuctions into variables that can be passed around. Let's create our program using delegates.

using System;

namespace FunctionalProgramming
{
    class Program
    {      
        delegate void SayGreeting(string name);

        static void Main(string[] args)
        {

        }
    }
}

Delegates is just a method without its functionality. Declaring its functionality with anonymous function helps us if we want to call it right away.

using System;

namespace FunctionalProgramming
{
    class Program
    {      
        delegate void SayGreeting(string name);

        static void Main(string[] args)
        {
          SayGreeting sayGreeting = delegate(string name)
          {
            Console.WriteLine(string.Format("Hello, {0}", name));
          };
        }
    }
}

Now, we make it into full conversation.

using System;

namespace FunctionalProgramming
{
    class Program
    {      
        delegate void SayGreeting(string name);

        static void Main(string[] args)
        {
          SayGreeting sayGreeting = delegate(string name)
          {
            Console.WriteLine(string.Format("Hello, {0}", name));
          };
          Console.WriteLine("What's your name?");
          var input = Console.ReadLine();
          sayGreeting(input);  
        }
    }
}

results matching ""

    No results matching ""