Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LambdaSyntaxScoping
- {
- // delegate double MyEvent(object sender, EventArgs args);
- delegate int MyLambda(int x, int y);
- class Program
- {
- static void Main(string[] args)
- {
- // syntax (static) scoping
- // free variable
- int a = 5;
- // https://msdn.microsoft.com/en-us/library/system.action(v=vs.110).aspx
- // Action Encapsulates a method that has no parameters and does not return a value.
- // You can use this delegate to pass a method as a parameter without explicitly declaring a custom delegate.
- // Action Action<T> Action>T1,T2> https://www.dotnetperls.com/action
- // Func<> Func receives parameters and returns a result value. https://www.dotnetperls.com/action
- // http://stackoverflow.com/questions/4317479/func-vs-action-vs-predicate
- // http://stackoverflow.com/questions/566860/delegates-predicate-action-func
- MyLambda myLambda = (x, y) => x * y * a;
- // or
- // Func<int, int, int> myLambda = (x, y) => x * y * a;
- Console.WriteLine(UseLambda(12, myLambda)); //720
- }
- // static int UseLambda(int b, Func<int, int, int> l) // with Func you can avoid declaring delegate
- // or
- static int UseLambda(int b, MyLambda l)
- {
- // dynamic Scoping. It is not used in "l"
- int a = 7; // the question is which "a" will be used in the lambda. It is not going to be this".
- return b * l(4, 3);
- }
- }
- }
- /*
- http://stackoverflow.com/questions/299703/delegate-keyword-vs-lambda-notation
- Once it is compiled, is there a difference between:
- delegate { x = 0; }
- and
- () => { x = 0 }
- ?
- Short answer : no.
- Longer answer that may not be relevant:
- If you assign the lambda to a delegate type (such as Func or Action) you'll get an anonymous delegate.
- If you assign the lambda to an Expression type, you'll get an expression tree instead of a anonymous delegate. The expression tree can then be compiled to an anonymous delegate.
- Edit: Here's some links for Expressions.
- System.Linq.Expression.Expression(TDelegate) (start here).
- Linq in-memory with delegates (such as System.Func) uses System.Linq.Enumerable. Linq to SQL (and anything else) with expressions uses System.Linq.Queryable. Check out the parameters on those methods.
- An Explanation from ScottGu. In a nutshell, Linq in-memory will produce some anonymous methods to resolve your query. Linq to SQL will produce an expression tree that represents the query and then translate that tree into T-SQL. Linq to Entities will produce an expression tree that represents the query and then translate that tree into platform appropriate SQL.
- http://stackoverflow.com/questions/566860/delegates-predicate-action-func
- Predicate: essentially Func<T, bool>; asks the question "does the specified argument satisfy the condition represented by the delegate?" Used in things like List.FindAll.
- Action: Perform an action given the arguments. Very general purpose. Not used much in LINQ as it implies side-effects, basically.
- Func: Used extensively in LINQ, usually to transform the argument, e.g. by projecting a complex structure to one property.
- Other important delegates:
- EventHandler/EventHandler<T>: Used all over WinForms
- Comparison<T>: Like IComparer<T> but in delegate form.
- Action, Func and Predicate all are belongs to delegate family.
- Action : Action can take n input parameters but it return void.
- Func : Func can take n input parameter but it will always return result of provided type. Func<T1,T2,T3,TResult>, here T1,T2,T3 are input parameters and TResult is the output of it.
- Predicate : Predicate is also a form of Func but it will always return bool. In simple words it is wrapper of Func<T,bool>.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement