Delegates To Lambdas Cheat Sheet

A cheat sheet Delegates & Lambdas in C#

All source code can be found on GitHub here.

This post is part of my cheat sheet series.

// DELEGATE DEFINITION
public delegate bool IsGreaterThan(int aParam, int compareTo);

// CREATION AND ASSIGNMENT

// .Net 1 version
IsGreaterThan isGreaterThanHandler = new IsGreaterThan(DelegatesToLambda.IsGreaterThanImplementation);

A cheat sheet for Ninject IOC Frmaework in C#

All source code can be found on GitHub <a href="https://github.com/lukewickstead/DOT-NET-on-Linux">here</a>.

// Method Group Conversion
IsGreaterThan isGreaterThanHandler = IsGreaterThanImplementation;

// Anonymous Function
IsGreaterThan isGreaterThanHandler = delegate(int aValue, int compareTo) { return aValue > compareTo; };

// Lambda Functions
IsGreaterThan isGreaterThanHandler = (int aValue, int compareTo) => { return aValue > compareTo; }; // Full hand
IsGreaterThan isGreaterThanHandler = (aValue, compareTo) => { return aValue > compareTo; }; // Param type inferrence
IsGreaterThan isGreaterThanHandler = (aValue, compareTo) => aValue > compareTo; // One line in method + return inferred
IsGreaterThan isGreaterThanHandler = aValue  => aValue > 1; // One param
IsGreaterThan isGreaterThanHandler = ()  => 2 > 1; // No params

// DELEGATES AND GENERICS

// Definition
public delegate string ConcatToString(T aValue, T compareTo);

// .Net 1 version
ConcatToString concatToStringHandler = new ConcatToString(DelegatesTestsWithGenerics.ConcatToStringImplementation);

// Method Group Conversion
ConcatToString concatToStringHandler = DelegatesTestsWithGenerics.ConcatToStringImplementation;

// Anonymous Function
ConcatToString concatToStringHandler = delegate(int aValue, int bValue) { return string.Format("{0}{1}", aValue, bValue); };

// Lambda Function
ConcatToString concatToStringHandle = (aValue, compareTo) => string.Format("{0}{1}", aValue, compareTo);

// FUNC<T, TResult>, ACTION PREDICATE

// Func, Action & Predicate are global delegates definitions to save creating your own.

// Func
// Func can take up to 16 parameter types and return one type

// Parameters expecting  a Func will looks something like
// Func
// Func<T, TResult>
// Func<T, T, TResult>
// Func<T, T, T, TResult>
// Func<T, T, T..... TResult>

Func<int, int, string> aHandler = delegate(int aInt, int bInt) { return string.Empty; }; // Anonymous Method
Func<int, int, string> cHandler = (anInt, anotherInt) => string.Empty; // Lambda

// Predicate can take up to 16 parameters and retun a bool
// Parameters expecting  a Predicate will looks something like
// Predicate<T, TResult>
// Predicate<T, T, TResult>
// Predicate<T, T, T, TResult>
// Predicate<T, T, T..... TResult>

Predicate aHandler = delegate(int anInt) { return anInt > 1; }; // Anonymous Method
Predicate cHandler = anInt => anInt > 1; // Lambda

// Action can take up to 16 parameters and retuns a null
// Parameters expecting  a Action will looks something like
// Predicate
// Predicate<T, T>
// Predicate<T, T, T>
// Predicate<T, T, T....., T>

Action aHandler = delegate(int x) { throw new Exception(); }; // Anonymous Method
Action bHandler = x => { throw new Exception(); };       // Lambda

// MULTICAST DELEGATES
// Multiple events can be registered and deregistered with += and -=
// All events are called one at a time.
// Last one returns the value.
IsGreaterThan isGreaterThanHandler = IsGreaterThanImplementation;
isGreaterThanHandler += IsGreaterOrEqual;
isGreaterThanHandler(1,2);
isGreaterThanHandler -= IsGreaterOrEqual;

// EVENTS
// Events are shorthand notation of grouping delegates
public delegate bool IsGreaterThan(int aParam, int compareTo);
public event IsGreaterThan IsGreaterThanEvent;
IsGreaterThanEvent += IsGreaterThanImplementation;
IsGreaterThanEvent += IsGreaterOrEqual;
IsGreaterThanEvent(1,2);
IsGreaterThanEvent -= IsGreaterOrEqual;

// CONTRAVARIANCE
Contravariance allows us to a method which has a parameter which is a child class of the defined parameter.

// COVARIANCE
Allows us to return a type which is a child class of the defined delegate return type

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s