Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. // 2b) Tyzden
  2. // 1) func, Func delegate + lambda, func + lambda !!!!!
  3. // 2) Lambda vyraz vs LINQ query
  4. // 3) Sinus
  5. // 4) More then two statements
  6.  
  7. // x <=> studenti koduju
  8.  
  9. // 0)
  10. // Pr. bez sluzobneho slova delegate - iba function, Func + anonimna metoda s lambda
  11. int func1(int x, int y) // 1) Funkcia
  12. {
  13. return 1 * x * y;
  14. }
  15. func1(5, 20).Dump("T0=");
  16.  
  17. // x1) Func delegate
  18. Func<int, int, int> func2 = (x, y) => 2 * x * y; // 2)
  19. func2(5, 20).Dump("T1=");
  20.  
  21. // !!!!!!!!!!!!!!!!
  22. // xBest
  23. int func3(int x, int y) => 3 * x * y; // 3)
  24. func3(5, 20).Dump("T3=");
  25.  
  26. Func<int, int, string> func9 = (x, y) => (9 * x * y).ToString(); // 9)
  27. Console.WriteLine(func9(5, 20));
  28.  
  29. // x2) Lambda vyraz vs LINQ query
  30. // var vs IEnumerable
  31. int[] ii = new[] { 1, 2, 3, 4, 6 };
  32. var v1 = ii.Where(i => i % 2 == 0); // lambda vyraz
  33. // <=>
  34. var v2 = from i in ii where i % 2 == 0 select i; // LINQ query - Language Integrated Query
  35. // <=>
  36. IEnumerable<int> v3 = ii.Where(i => i % 2 == 0);
  37. v1.Dump();
  38. v2.Dump();
  39. v3.Dump();
  40.  
  41. // x3) Sinus
  42. double[] xx = new[] { 0, 1.6, 3.14, Math.PI / 2, Math.PI };
  43. xx.Select(x => Math.Sin(x)).Dump();
  44. // BEST: !!!!!
  45. xx.Select(Math.Sin).Dump();
  46. //xx.Select(x => {x++;x--;Math.Sin(x);}).Dump();
  47. // <=>
  48. var si = from x in xx select Math.Sin(x);
  49. si.Dump();
  50.  
  51. // 4) More then two statements
  52. Func<int, int, int> fu1 = (x, y) => { int z = x * y; string s ="tata";
  53. if (true){z++; z--;} return z + z; }; // 2)
  54. fu1(5, 20).Dump("T1=");
  55.  
  56. // xBetter
  57. int fu2(int x, int y){ int z = x * y; string s = "tata";
  58. if (true) { z++; z--; } return z + z; };
  59. fu2(5, 20).Dump("T1=");
  60.  
  61.  
  62. /// NONO
  63. //int fu3(int x, int y) => { int z = x * y; return z + z; }; // 2)
  64. //NO//fu2(5, 20).Dump("T1=");
  65.  
  66. xx.Select(x => x + x).Dump();
  67. //NO//xx.Select(x => { x+x;}).Dump();
  68.  
  69.  
  70.  
  71. // 5)
  72. // Pole
  73. // string[] stud = new[] { "Fero", "Jano", "Stevo" };
  74. //
  75. // Var & List & anonymns
  76. // var studs1 = new List<object>
  77. // {
  78. // new {Krstne="Bob", Priezvisko="Bib" },
  79. // new {Krstne="Bobi", Priezvisko="BiBi"}
  80. // };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement