Linq

Linq homepage (link).

Linq is based on concepts of Generic Delegates and Lambda Expressions. In addition, extension methods, type inference, anonymous types, object initializers, and custom iterators also form the base for Linq.

    • Anonymous method = method without a name, usually declared inline.
    • Generics (<T>)
    • Generic Delegates named Func.
    • Extension Methods
    • yield (link)

Delegates

Delegates are classes, constructed in the background by the compiler, that allow type-safe callback mechanism in .NET.

Predicates

Predicates are delegate functions that return a boolean.

Lambda

The main point of lambda expressions is that they remove a level of indirection from within the source code.

Lambda operator '=>' (pronounced "goes to"). Syntax example: Thread x = new Thread( (object mtp) => { method body } ). The part "(object mtp)" shows the list of parameters. Lambda operator points that we want a method to be created, which accepts the listed parameter(s).

There are some rules that have to be followed when using Lambda operator "=>". The left side of the operator is where the arguments are specified by name.

    • For delegates that take no arguments, you can use ()
    • Func<String> func = () => "Seven";
    • For delegates that take multiple arguments, you can specify the types
    • Func<Int32, String> func = (Int32 arg) => arg.ToString();
    • For delegates that take multiple arguments, the compiler can infer the type(s)
    • Func<Int32, String> func = (arg) => arg.ToString();
    • For delegates that take 1 argument, () can be omitted
    • Func<Int32, String> func = arg => arg.ToString();
    • For delegates that have ref/out arguments, these must be explicitly specified together with the type
    • Dlgt d = (out Int32 arg) => arg = 5;
    • where Dlgt is defined as
    • delegate void Dlgt(out Int32 arg);

Linq to XML

5 Minute overview (link).

Links & References

    • Generic Delegates and Lambda Expressions (link)
    • Linq Expressions – From and to delegates (link)
    • Anonymous Methods (link) video series by Peter Thorsteinson. Channel also contains good introduction to Delegates. Covariance and contra variance video (link).
    • Anonymous Methods on MSDN (link)