Funcs
It just run a function and doesn't have a return value. How if we want to have a return value? We use Func method.
Func<T, TResult>
Func<T, T1, T2, TResult>
using System;
namespace FunctionalProgramming
{
class Program
{
static void Main(string[] args)
{
Action<string> sayGreeting;
Func<string, string> conversate = delegate (string message)
{
Console.WriteLine(message);
return Console.ReadLine();
};
string input = conversate("What's your name?");
}
}
}
So, our input
variable contains the return value which is whatever you type on console.
Let's make it a full conversation.
using System;
namespace FunctionalProgramming
{
class Program
{
static void Main(string[] args)
{
Action<string> sayGreeting;
Func<string, string> conversate = delegate (string message)
{
Console.WriteLine(message);
return Console.ReadLine();
};
string input = conversate("What's your name?");
sayGreeting = delegate (string greeting)
{
Console.WriteLine(string.Format(greeting, input);
};
sayGreeting("Hello, {0}");
sayGreeting("Bye, {0}");
}
}
}