Tutorial by Examples

When assigning named methods to delegates, they will refer to the same underlying object if: They are the same instance method, on the same instance of a class They are the same static method on a class public class Greeter { public void WriteInstance() { Console.Write...
The following syntax creates a delegate type with name NumberInOutDelegate, representing a method which takes an int and returns an int. public delegate int NumberInOutDelegate(int input); This can be used as follows: public static class Program { static void Main() { Number...
The System namespace contains Func<..., TResult> delegate types with between 0 and 15 generic parameters, returning type TResult. private void UseFunc(Func<string> func) { string output = func(); // Func with a single generic type parameter returns that type Console.WriteLine...
Named methods can be assigned to delegates with matching signatures: public static class Example { public static int AddOne(int input) { return input + 1; } } Func<int,int> addOne = Example.AddOne Example.AddOne takes an int and returns an int, its signature ...
Calling .Equals() on a delegate compares by reference equality: Action action1 = () => Console.WriteLine("Hello delegates"); Action action2 = () => Console.WriteLine("Hello delegates"); Action action1Again = action1; Console.WriteLine(action1.Equals(action1)) // True ...
Lambdas can be used to create anonymous methods to assign to a delegate: Func<int,int> addOne = x => x+1; Note that the explicit declaration of type is required when creating a variable this way: var addOne = x => x+1; // Does not work
Delegates can be used as typed function pointers: class FuncAsParameters { public void Run() { DoSomething(ErrorHandler1); DoSomething(ErrorHandler2); } public bool ErrorHandler1(string message) { Console.WriteLine(message); var shouldWeContinue = ... re...
Addition + and subtraction - operations can be used to combine delegate instances. The delegate contains a list of the assigned delegates. using System; using System.Reflection; using System.Reflection.Emit; namespace DelegatesExample { class MainClass { private delegate void MyD...
Ever wanted to call a multicast delegate but you want the entire invokation list to be called even if an exception occurs in any in the chain. Then you are in luck, I have created an extension method that does just that, throwing an AggregateException only after execution of the entire list complete...
Closures are inline anonymous methods that have the ability to use Parent method variables and other anonymous methods which are defined in the parent's scope. In essence, a closure is a block of code which can be executed at a later time, but which maintains the environment in which it was firs...
public class MyObject{ public DateTime? TestDate { get; set; } public Func<MyObject, bool> DateIsValid = myObject => myObject.TestDate.HasValue && myObject.TestDate > DateTime.Now; public void DoSomething(){ //We can do this: if(this.TestDate....

Page 1 of 1