Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lambda Expressions Version 1.0

Similar presentations


Presentation on theme: "Lambda Expressions Version 1.0"— Presentation transcript:

1 Lambda Expressions Version 1.0
C# .Net Software Development Lambda Expressions Version 1.0

2 Overview Lambda Expressions (definition) Rules
Func<…> , Action<…> & Predicate<…> Generic Delegates x => x or (T x) => x + 10 Type Inference Scope & Lifetime Copyright © 2008 by Dennis A. Fairclough all rights reserved.

3 Lambda Expressions “C# 2.0 introduced anonymous methods, allowing code blocks to be written “in-line” where delegate values are expected. Anonymous methods provide much of the expressive power of functional programming languages, the anonymous method syntax is rather verbose and imperative in nature. Lambda expressions provide a more concise, functional syntax for writing anonymous methods.” C# 3.0 Specification Copyright © 2008 by Dennis A. Fairclough all rights reserved.

4 Lambda Expressions – C# Spec
A lambda-expression creates a declaration space which contains the parameters of the anonymous function. The scope of a parameter declared in a lambda-expression is the lambda-expression- body of that lambda-expression The scope of a parameter declared in an anonymous-method-expression is the block of that anonymous-method-expression. Copyright © 2008 by Dennis A. Fairclough all rights reserved.

5 Lambda Expressions – C# Spec.
For almost all purposes, lambda-expressions are more concise and expressive than anonymous-method-expressions. lambda-expression: anonymous-function-signature => anonymous-function-body Copyright © 2008 by Dennis A. Fairclough all rights reserved.

6 Lambda Expressions – C# Spec.
The behavior of lambda-expressions and anonymous- method-expressions is the same except for the following points: anonymous-method-expressions permit the parameter list to be omitted entirely, yielding convertibility to delegate types of any list of value parameters. lambda-expressions permit parameter types to be omitted and inferred whereas anonymous-method-expressions require parameter types to be explicitly stated. The body of a lambda-expression can be an expression or a statement block whereas the body of an anonymous-method-expression must be a statement block. Since only lambda-expressions can have an expression body, no anonymous-method-expression can be successfully converted to an expression tree type (§4.6). Copyright © 2008 by Dennis A. Fairclough all rights reserved.

7 Lambda Expressions “A lambda expression is written as a parameter list, followed by the => Lambda Operator, followed by an expression or a statement block.” C# 3.0 Specification (param list) => (expression or block); Lambda Expressions x => x * 3; same as (x) => x * 3; //single parameter (x, y) => { x = x + y; return x; }; //two parameters () => Console.WriteLine(“No parameters”); Copyright © 2008 by Dennis A. Fairclough all rights reserved.

8 Lambda Expression Scope
Reference Local Variables and Parameters of the method they are defined in. Example int idata = 100; = () => idata++; Copyright © 2008 by Dennis A. Fairclough all rights reserved.

9 Lambda Expression Rules C# 3.0 Specification
In general, the specification of anonymous methods, provided in the C# 2.0 Specification, also applies to lambda expressions. Lambda expressions are a functional superset of anonymous methods, providing the following additional functionality: Lambda expressions permit parameter types to be omitted and inferred whereas anonymous methods require parameter types to be explicitly stated. The body of a lambda expression can be an expression or a statement block whereas the body of an anonymous method can only be a statement block. Lambda expressions passed as arguments participate in type argument inference and in method overload resolution. Lambda expressions with an expression body can be converted to expression trees. Copyright © 2008 by Dennis A. Fairclough all rights reserved.

10 Func, Action & Predicate Delegates
Func Delegate public delegate TResult Func<T, TResult>( T arg ) Has T1 through T16 arguments Func<int,int,bool> fdel = (x,y)=>x<y; bool status = fdel(5,6); //status = true Action Delegate public delegate void Action<T>( T obj ) Action<int,int> adel = (a,b) => Console.WriteLine(a+b); adel(6,7); Predicate Delegate returns a bool and takes 1 param public delegate bool Predicate<T>(T pred) Predicate<int> pdel = x => x>20; bool status = pdel(15); bCopyright © 2008 by Dennis A. Fairclough all rights reserved.

11 Lambda Expression Type Inference
When a generic method is called without specifying type arguments, a type inference process attempts to infer type arguments for the call. Lambda expressions passed as arguments to the generic method participate in this type inference process. If type inference is indeterminate the user must supply the type. Copyright © 2008 by Dennis A. Fairclough all rights reserved.

12 Expression Tree C# 3.0 in a Nutshell
Expression<T> representation of the code inside the lambda expression. Is a traversable object model allowing the lambda expression to be interpreted at runtime. Copyright © 2008 by Dennis A. Fairclough all rights reserved.

13 What did you learn? ?? Copyright © 2008 by Dennis A. Fairclough all rights reserved.


Download ppt "Lambda Expressions Version 1.0"

Similar presentations


Ads by Google