xxxxxxxxxx
// Delegate via anonymous function or lambda expression
// 1. Define delegate type
delegate string Greeting(string name); // An alternative to defining a delegate type that returns a result is just use built-in delegate type Func<T1,..,TResult>
class Program
{
static void Main()
{
// 2a. Create a variable of the delegate type via anonymous function
Greeting greetA = delegate (string name)
{
return $"Hello {name}!";
};
// 2b. Create a variable of built-in delegate type Func<T1,..,TResult> instead of the custom defined delegate type via lambda expression
Func<string, string> greetB = (name) =>
{
return $"Hello {name}!";
};
// 3. Usage
Console.WriteLine(greetA("Anonymous"));
Console.WriteLine(greetB("Lambda"));
}
}