hrt

20
Lambda Expressions in C# From Beginner To Expert Jaliya Udagedara

TAGS:

description

retjhrt

Transcript of hrt

Page 1: hrt

Lambda Expressions in C# From Beginner To Expert

Jaliya Udagedara

Page 2: hrt

What are we going to discuss today?Introduction to Lambda ExpressionsWhat are Delegates in C#The Evolution of Delegates in C#Demo 1 - Delegates and Named Methods, Anonymous MethodsLambdas as Generic Delegates

ActionFuncPredicate

Lambdas as Callable EntitiesLambda Expression Execution

Lambdas as CallbacksDemo 2 – Lambda Expressions

Page 3: hrt

Introduction to Lambda ExpressionsIntroduced with .NET 3.5/C# 3.0Supersede anonymous methods

Anonymous methods enable you to omit the parameter listUnnamed, inline functionsUsed wherever delegates are required

(input parameters) => expression

Page 4: hrt

Expression Lambdas(input parameters) => expression

Statement Lambdas(input parameters) => {statement;}

Async Lambdas(input parameters) => async

{statement;}

Page 5: hrt

What are Delegates in C#Delegates are like C++ function pointersMakes it possible to treat methods as entities.

Assign to variablesPass as parameters

Delegates can be chained together; for example, multiple methods can be called on a single event.Does delegates are exactly same as C++ function pointers?Does methods has to match the delegate type exactly?

Page 6: hrt

What are Delegates in C# contd.Delegates are like C++ function pointers but are type safe.Variance in Delegates

Covariance permits a method to have return type that is More derived than that defined in the delegate

Contravariance permits a method that has parameter types that are less derived than those in the delegate type.

Page 7: hrt

The Evolution of Delegates in C#C# 1.0 the only way to declare a delegate was to use named methods.C# 2.0 introduced anonymous methods as a way to write unnamed inline statement blocks that can be executed in a delegate invocation.C# 3.0 introduced lambda expressions, which are similar in concept to anonymous methods but more expressive and concise.

Page 8: hrt

Classic Delegate Example

btnHelloWorld.Click += new

EventHandler(btnHelloWorld_Click);

Page 9: hrt

Anonymous MethodsbtnHelloWorld.Click += delegate(System.Object sender, System.EventArgs e) { … };

Lambda ExpressionsbtnHelloWorld.Click += (sender, e) => { ... };

Page 10: hrt

Demo• Delegates and Named

Methods• Anonymous Methods

Page 11: hrt

Lambdas as Generic DelegatesGeneric delegates were new in .NET 2.0Action DelegatesFunc DelegatesPredicate Delegates

Page 12: hrt

Action DelegateZero, one or more input parameters, and does not return anything.Takes up to 16 parametersArray.ForEach method and List.ForEachPerform an action on each element of the array or list

Action<int,int,string>

Page 13: hrt

Func DelegateZero, one or more input parameters, and returns a valueTakes up to 16 parametersList.First

Func<int,int,string>

Page 14: hrt

Predicate DelegateEncapsulates a method that evaluates to True or FalseTakes one parameterList.Find

Func<int>

Page 15: hrt

Lambdas as Callable EntitiesLambda expressions can be assigned to a delegate variableLambda expression is executed when the delegate is executed Func<int, string> myFunc = x => { return String.Format("{0}*{0} is {1}", x, x * x); }; Console.WriteLine(myFunc(4));

Page 16: hrt

Lambda Expression ExecutionLambda expressions are executed when they are called, not when they are constructedVariable value used is the value at execution time

Page 17: hrt

Example: Local Variablesint y = 0;Func<int, string> myFunc = x => (x + y).ToString();y = 10;Console.WriteLine(myFunc(5));

Answer?

Page 18: hrt

Lambdas as CallbacksCallback

“Executable code that is passed as an argument to other code”Passes a function to a function static void DoWork() { CallBack(s => Console.WriteLine(s)); }

static void CallBack(Action<string> MyAction) { MyAction("Completed"); }

Page 19: hrt

Demo• Lambdas as Generic Delegates• Lambdas as Callable Entities• Lambdas as Callbacks

Page 20: hrt

Thank You!http://www.jaliyaudagedara.blogspot.com/