Skip to content

Instantly share code, notes, and snippets.

View mattumotu's full-sized avatar

Matt Phillips mattumotu

  • United Kingdom
View GitHub Profile
// Generic example
public delegate return-type myDelegate(<ParameterList>);
// Concrete example
public delegate void responseBehaviour(Person borrower);
// matchingMethod1 matches myDelegate delegate
public return-type matchingMethod1(<ParameterList>) { ... }
// matchingMethod2 matches myDelegate delegate
public return-type matchingMethod2(<ParameterList>) { ... }
// askPolitely matches responseBehaviour delegate
public void askPolitely(Person borrower) {
borrower.say("Y-- Excuse me. You-- I believe you have my stapler?");
}
// Generic example
public void myMethod(myDelegate paramName) { ... }
// encapsulate matchingMethod1 in myDelegate and pass into myMethod
myMethod(new myDelegate(matchingMethod1));
// alternative syntax for matchingMethod2
myMethod(matchingMethod2);
// Concrete example
public void borrowStapler(responseBehaviour lateResponse) { ... }
// Action declarations
public delegate void Action();
public delegate void Action<in T>(T arg);
public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);
// ...
public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16);
// Func declarations
public delegate TResult Func<out TResult>();
public delegate TResult Func<in T, out TResult>(T arg);
public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
...
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16);
// Predicate declaration
public delegate bool Predicate<in T>(T arg);
public class ClientFetcher {
public Client Fetch() { ... };
}
public class ProjectFetcher{
public Project Fetch() { ... };
}
public interface IFetchThings {
Client Fetch();
}
public class ClientFetcher : IFetchThings {
public Client Fetch() { ... }; // implements IFetchThings.Fetch()
}
public class ProjectFetcher : IFetchThings {
public Project Fetch() { ... }; // Does not implement IFetchThings.Fetch() as they have differing return types. A Project isn't a Client.
}