Lambda Expressions Version 1.0

Slides:



Advertisements
Similar presentations
Developer Knowledge Sharing Eric Sun Dec, What programming language did you learn in school and since then? Now, its time to refresh …
Advertisements

Lambda Expressions Matt Van Horn Software Engineer Iverson Gaming Systems, Inc.
Introduction to C Programming
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.
Operator Overloading and Type Conversions
Eric Vogel Software Developer A.J. Boggs & Company.
Advanced .NET Programming I 13th Lecture
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
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.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
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.
 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.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
Python Functions.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
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.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
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,
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
CS212: Object Oriented Analysis and Design Lecture 30: Namespace and Other Advanced Topics.
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.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
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.
Lambda Expressions Version 1.1
Chapter 7: User-Defined Functions II
6.001 SICP Variations on a Scheme
Lambda Expressions By Val Feldsher.
CS 2304 Function Pointers & Lambda Functions
Corky Cartwright January 18, 2017
Function There are two types of Function User Defined Function
C++ for Engineers and Scientists Second Edition
6 Delegate and Lambda Expressions
Dynamic Scoping Lazy Evaluation
6.001 SICP Further Variations on a Scheme
G. Pullaiah College of Engineering and Technology
IT College 2016, Andres käver
Advanced .NET Programming I 4th Lecture
Corresponds with Chapter 5
Parameters and Arguments
Scope Rules.
Presentation transcript:

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

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

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.