13
LAMBDA EXPRESSIONS VERSION 1.0 C# .Net Software Development

C#.Net Software Development. Overview Lambda Expressions (definition) Rules Func, Action & Predicate Generic Delegates x => x+ 10 or (T x) =>

Embed Size (px)

Citation preview

Page 1: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

LAMBDA EXPRESSIONS

VERSION 1.0

C# .Net Software Development

Page 2: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

Overview

Lambda Expressions (definition) Rules Func<…> , Action<…> &

Predicate<…> Generic Delegates x => x+ 10 or (T x) => x + 10 Type Inference Scope & Lifetime

2Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Page 3: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

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

3Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Page 4: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

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. 4

Page 5: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

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. 5

Page 6: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

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. 6

Page 7: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

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”);

7Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Page 8: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

Lambda Expression Scope

Lambda Expressions 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. 8

Page 9: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

Lambda Expression RulesC# 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. 9Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Page 10: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

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 Has T1 through T16 arguments

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. 10

Page 11: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

Lambda ExpressionType 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. 11

Page 12: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

Expression TreeC# 3.0 in a Nutshell

Expression tree 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. 12

Page 13: C#.Net Software Development. Overview  Lambda Expressions (definition)  Rules  Func, Action & Predicate Generic Delegates  x => x+ 10 or (T x) =>

What did you learn?

??

Copyright © 2008 by Dennis A. Fairclough all rights reserved. 13