Transcript
Page 1: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

Lambda Expressions in C# From Beginner To Expert

Jaliya Udagedara

Page 2: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

Expression Lambdas(input parameters) => expression

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

Async Lambdas(input parameters) => async

{statement;}

Page 5: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

Classic Delegate Example

btnHelloWorld.Click += new

EventHandler(btnHelloWorld_Click);

Page 9: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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

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

Page 10: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

Demo• Delegates and Named

Methods• Anonymous Methods

Page 11: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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

Page 12: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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

Func<int,int,string>

Page 14: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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

Func<int>

Page 15: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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

Answer?

Page 18: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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: Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

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

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

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


Top Related