Lambda Expressions Version 1.1

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Chapter Five Functions
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 5 Functions.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Eric Vogel Software Developer A.J. Boggs & Company.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CPS120: Introduction to Computer Science Lecture 14 Functions.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Python Functions.
Lambda Expressions Version 1.0
C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
Attributes C#.Net Software Development Version 1.0.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Advanced .NET Programming I 3nd Lecture
Chapter 7: User-Defined Functions II
6.001 SICP Variations on a Scheme
Lambda Expressions By Val Feldsher.
Chapter 14 Templates C++ How to Program, 8/e
Upgrading Your C# Programming Skills to Be a More Effective Developer
Advanced .NET Programming I 4th Lecture
CS 2304 Function Pointers & Lambda Functions
Corky Cartwright January 18, 2017
Function There are two types of Function User Defined Function
Method.
Delegates/ Anders Børjesson
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
.NET and .NET Core: Languages, Cloud, Mobile and AI
A brief look at some of the new features introduced to the language
Lecture 3 Functions Simple functions
Operator Overloading
6 Delegate and Lambda Expressions
FP Foundations, Scheme In Text: Chapter 14.
Chapter 4 Writing Classes.
Dynamic Scoping Lazy Evaluation
LINQ & ADO.NET Entity Framework
6.001 SICP Further Variations on a Scheme
Peter Seibel Practical Common Lisp Peter Seibel
Classes, Objects and Methods
Advanced .NET Programming I 5th Lecture
Standard Version of Starting Out with C++, 4th Edition
Advanced .NET Programming I 4th Lecture
Corresponds with Chapter 5
Events, Delegates, and Lambdas
Parameters and Arguments
Scope Rules.
Presentation transcript:

Lambda Expressions Version 1.1 C# .Net Software Development Lambda Expressions Version 1.1

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

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.

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.

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.

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.

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.

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.

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.

Func, Action & Predicate Delegates Func Delegate public delegate TResult Func<T, TResult>( T arg ) Has T1 through T4 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.

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.

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.

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