why we use delegate Func there...
using System;
using System;
class Test
{
static void Main()
{
Func func = GetGreeting; // no parameters but returns a string
Console.WriteLine(func());
Console.ReadKey();
}
static string GetGreeting()
{
return "Hello Jitendra :)";
}
}
Ramesh MaruthiPosted Jul 28, 2014, 10:21 AM
public delegate T Func
Its value can be assigned to a named method or to an anonymous method through delegate syntax or through lambda expression syntax. All the following assignments are correct and return the same result:
void Main()
{
Func
theDelegate = NamedMethod; // Assign to a named method.
theDelegate = delegate() { return 0; }; // Assign to anonymous method through delegate syntax.
theDelegate = delegate { return 0; }; // It has no parameters, so round braces can be omitted.
theDelegate = () => { return 0; }; // The same anonymous method through lambda expression syntax.
theDelegate = () => 0; // The syntax of single return statement can be even more simpler.
int result = theDelegate(); // Call the delegate and get the result.
}
int NamedMethod()
{
return 0;
}