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

Download Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara

Post on 26-Aug-2014

709 views

Category:

Software

13 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

  • Lambda Expressions in C# From Beginner To Expert Jaliya Udagedara
  • What are we going to discuss today? Introduction to Lambda Expressions What are Delegates in C# The Evolution of Delegates in C# Demo 1 - Delegates and Named Methods, Anonymous Methods Lambdas as Generic Delegates Action Func Predicate Lambdas as Callable Entities Lambda Expression Execution Lambdas as Callbacks Demo 2 Lambda Expressions
  • Introduction to Lambda Expressions Introduced with .NET 3.5/C# 3.0 Supersede anonymous methods Anonymous methods enable you to omit the parameter list Unnamed, inline functions Used wherever delegates are required (input parameters) => expression
  • Expression Lambdas (input parameters) => expression Statement Lambdas (input parameters) => {statement;} Async Lambdas (input parameters) => async {statement;}
  • What are Delegates in C# Delegates are like C++ function pointers Makes it possible to treat methods as entities. Assign to variables Pass 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?
  • 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.
  • 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.
  • Classic Delegate Example btnHelloWorld.Click += new EventHandler(btnHelloWorld_Click);
  • Anonymous Methods btnHelloWorld.Click += delegate(System.Object sender, System.EventArgs e) { }; Lambda Expressions btnHelloWorld.Click += (sender, e) => { ... };
  • Anonymous
  • Lambdas as Generic Delegates Generic delegates were new in .NET 2.0 Action Delegates Func Delegates Predicate Delegates
  • Action Delegate Zero, one or more input parameters, and does not return anything. Takes up to 16 parameters Array.ForEach method and List.ForEach Perform an action on each element of the array or list Action
  • Func Delegate Zero, one or more input parameters, and returns a value Takes up to 16 parameters List.First Func
  • Predicate Delegate Encapsulates a method that evaluates to True or False Takes one parameter List.Find Func
  • Lambdas as Callable Entities Lambda expressions can be assigned to a delegate variable Lambda expression is executed when the delegate is executed Func myFunc = x => { return String.Format("{0}*{0} is {1}", x, x * x); }; Console.WriteLine(myFunc(4));
  • Lambda Expression Execution Lambda expressions are executed when they are called, not when they are constructed Variable value used is the value at execution time
  • Example: Local Variables int y = 0; Func myFunc = x => (x + y).ToString(); y = 10; Console.WriteLine(myFunc(5)); Answer?
  • Lambdas as Callbacks Callback 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 MyAction) { MyAction("Completed"); }
  • Lambdas as Generic Delegates Lambdas as Callable Entities Lambdas as Callbacks
  • Thank You! http://www.jaliyaudagedara.blogspot.com/

Recommended

View more >