Simple_lamba_object1.htm
Key points:
  • A lambda expression , by construction, is an anonymous function that can be passed or returned as an value. It is used in LINQ and delegates extensively.
    • x => x * 5; // is an lambda expression
    • var var1 = x => x * 5;
      • // var1 object gets lambda product
    • delegate void Dlg1(sting str1);//
  • A delegate in C# is used to encapsulate a method and very is similar to a function pointer in C or C++
  • "A delegate  in C# does not know or care about the class of the object that it references." --msdn 

    • "Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation. " --msdn

  •  System.Linq.Expressions, contains classes to create expression trees based on lambda expressions
    • emp.Where(c => c.dept == "SALES");
    • List<ObjectFacotry> empdb = getEmp();
    • IEnumerable<ObjectFacotry> query1 = empdb.Where(emp => emp.job.Contains("MANAG"));
    • Consider this C# script : LINQ_Contains1.txt
  • More on LINQ :
Simple_lamba_object1.cs
using System;
using System.Linq;
using System.Linq.Expressions;
// Simple_lamba_object1.cs

namespace ExpressionTreeThingy
{
class Program
{
static void Main(string[] args)
{
//this is an object. extended with LINQ 
Expression<Func<int, int>> expr = (x) => x * 5; 
//compiles the object to a CLR delegate, at runtime
var var1 = expr.Compile();
//invoking a delegate at this point
Console.WriteLine(var1(5));
Console.ReadKey();
}
}
}