Download - htrfger

Transcript
Page 1: htrfger

Lambda Expressions in C# From Beginner To Expert

Jaliya Udagedara

Page 2: htrfger

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: htrfger

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: htrfger

Expression Lambdas(input parameters) => expression

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

Async Lambdas(input parameters) => async

{statement;}

Page 5: htrfger

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: htrfger

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: htrfger

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: htrfger

Classic Delegate Example

btnHelloWorld.Click += new

EventHandler(btnHelloWorld_Click);

Page 9: htrfger

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

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

Page 10: htrfger

Demo• Delegates and Named

Methods• Anonymous Methods

Page 11: htrfger

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

Page 12: htrfger

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: htrfger

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

Func<int,int,string>

Page 14: htrfger

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

Func<int>

Page 15: htrfger

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: htrfger

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: htrfger

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

Answer?

Page 18: htrfger

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: htrfger

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

Page 20: htrfger

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