Advertisement
wingman007

LambdaSyntaxScoping

Sep 17th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace LambdaSyntaxScoping
  8. {
  9.     // delegate double MyEvent(object sender, EventArgs args);
  10.     delegate int MyLambda(int x, int y);
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             // syntax (static) scoping
  16.             // free variable
  17.             int a = 5;
  18.  
  19.             // https://msdn.microsoft.com/en-us/library/system.action(v=vs.110).aspx
  20.             // Action Encapsulates a method that has no parameters and does not return a value.
  21.             // You can use this delegate to pass a method as a parameter without explicitly declaring a custom delegate.
  22.             // Action Action<T> Action>T1,T2> https://www.dotnetperls.com/action
  23.             // Func<> Func receives parameters and returns a result value. https://www.dotnetperls.com/action
  24.             // http://stackoverflow.com/questions/4317479/func-vs-action-vs-predicate
  25.             // http://stackoverflow.com/questions/566860/delegates-predicate-action-func
  26.  
  27.             MyLambda myLambda = (x, y) => x * y * a;
  28.             // or
  29.             // Func<int, int, int> myLambda = (x, y) => x * y * a;
  30.  
  31.             Console.WriteLine(UseLambda(12, myLambda)); //720
  32.  
  33.         }
  34.  
  35.         // static int UseLambda(int b, Func<int, int, int> l) // with Func you can avoid declaring delegate
  36.         // or
  37.         static int UseLambda(int b, MyLambda l)
  38.         {
  39.             // dynamic Scoping. It is not used in "l"
  40.             int a = 7; // the question is which "a" will be used in the lambda. It is not going to be this".
  41.             return b * l(4, 3);
  42.         }
  43.     }
  44. }
  45.  
  46. /*
  47. http://stackoverflow.com/questions/299703/delegate-keyword-vs-lambda-notation
  48.  
  49. Once it is compiled, is there a difference between:
  50.  
  51. delegate { x = 0; }
  52. and
  53.  
  54. () => { x = 0 }
  55. ?
  56.  
  57. Short answer : no.
  58.  
  59. Longer answer that may not be relevant:
  60.  
  61. If you assign the lambda to a delegate type (such as Func or Action) you'll get an anonymous delegate.
  62. 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.
  63. Edit: Here's some links for Expressions.
  64.  
  65. System.Linq.Expression.Expression(TDelegate) (start here).
  66. 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.
  67. 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.
  68.  
  69. http://stackoverflow.com/questions/566860/delegates-predicate-action-func
  70. 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.
  71. Action: Perform an action given the arguments. Very general purpose. Not used much in LINQ as it implies side-effects, basically.
  72. Func: Used extensively in LINQ, usually to transform the argument, e.g. by projecting a complex structure to one property.
  73. Other important delegates:
  74.  
  75. EventHandler/EventHandler<T>: Used all over WinForms
  76. Comparison<T>: Like IComparer<T> but in delegate form.
  77.  
  78.  
  79. Action, Func and Predicate all are belongs to delegate family.
  80.  
  81. Action : Action can take n input parameters but it return void.
  82.  
  83. 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.
  84.  
  85. Predicate : Predicate is also a form of Func but it will always return bool. In simple words it is wrapper of Func<T,bool>.
  86. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement