Tutorial by Examples

This example shows how to create a delegate that encapsulates the method that returns the current time static DateTime UTCNow() { return DateTime.UtcNow; } static DateTime LocalNow() { return DateTime.Now; } static void Main(string[] args) { Func<DateTime> method = U...
static int Sum(int a, int b) { return a + b; } static int Multiplication(int a, int b) { return a * b; } static void Main(string[] args) { Func<int, int, int> method = Sum; // method points to the Sum method // that retuns 1 int variable and takes 2 int vari...
An anonymous method can be assigned wherever a delegate is expected: Func<int, int> square = delegate (int x) { return x * x; } Lambda expressions can be used to express the same thing: Func<int, int> square = x => x * x; In either case, we can now invoke the method stored ins...
Func also supports Covariant & Contravariant // Simple hierarchy of classes. public class Person { } public class Employee : Person { } class Program { static Employee FindByTitle(String title) { // This is a stub for a method that returns // an employee that h...

Page 1 of 1