2. 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
3. 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
5. 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?
6. 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.
7. 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.
11. Lambdas as Generic Delegates
Generic delegates were new in .NET 2.0
Action Delegates
Func Delegates
Predicate Delegates
12. 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<int,int,string>
13. Func Delegate
Zero, one or more input parameters, and returns a
value
Takes up to 16 parameters
List.First
Func<int,int,string>
15. Lambdas as Callable Entities
Lambda expressions can be assigned to a delegate
variable
Lambda expression is executed when the delegate
is executed
Func<int, string> myFunc = x =>
{
return String.Format("{0}*{0} is {1}", x, x * x);
};
Console.WriteLine(myFunc(4));
16. 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
17. Example: Local Variables
int y = 0;
Func<int, string> myFunc = x => (x +
y).ToString();
y = 10;
Console.WriteLine(myFunc(5));
Answer?
18. 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<string> MyAction)
{
MyAction("Completed");
}
19. • Lambdas as Generic Delegates
• Lambdas as Callable Entities
• Lambdas as Callbacks
Works with a variety of database servers (including Microsoft SQL Server, Oracle, and DB2)Includes a rich mapping engine that can handle real-world database schemas and works well with stored proceduresProvides integrated Visual Studio tools to visually create entity models and to auto-generate models from an existing database. New databases can be deployed from a model, which can also be hand-edited for full controlProvides a Code First experience to create entity models using code. Code First can map to an existing database or generate a database from the model.Integrates well into all the .NET application programming models including ASP.NET, Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), and WCF Data Services (formerly ADO.NET Data Services)
Reduced development time: the framework provides the core data access capabilities so developers can concentrate on application logic.Developers can work in terms of a more application-centric object model, including types with inheritance, complex members, and relationships. In .NET Framework 4, the Entity Framework also supports Persistence Ignorance through Plain Old CLR Objects (POCO) entities.Applications are freed from hard-coded dependencies on a particular data engine or storage schema by supporting a conceptual model that is independent of the physical/storage model.Mappings between the object model and the storage-specific schema can change without changing the application code.Language-Integrated Query support (called LINQ to Entities) provides IntelliSense and compile-time syntax validation for writing queries against a conceptual model.
Reduced development time: the framework provides the core data access capabilities so developers can concentrate on application logic.Developers can work in terms of a more application-centric object model, including types with inheritance, complex members, and relationships. In .NET Framework 4, the Entity Framework also supports Persistence Ignorance through Plain Old CLR Objects (POCO) entities.Applications are freed from hard-coded dependencies on a particular data engine or storage schema by supporting a conceptual model that is independent of the physical/storage model.Mappings between the object model and the storage-specific schema can change without changing the application code.Language-Integrated Query support (called LINQ to Entities) provides IntelliSense and compile-time syntax validation for writing queries against a conceptual model.
Reduced development time: the framework provides the core data access capabilities so developers can concentrate on application logic.Developers can work in terms of a more application-centric object model, including types with inheritance, complex members, and relationships. In .NET Framework 4, the Entity Framework also supports Persistence Ignorance through Plain Old CLR Objects (POCO) entities.Applications are freed from hard-coded dependencies on a particular data engine or storage schema by supporting a conceptual model that is independent of the physical/storage model.Mappings between the object model and the storage-specific schema can change without changing the application code.Language-Integrated Query support (called LINQ to Entities) provides IntelliSense and compile-time syntax validation for writing queries against a conceptual model.