fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class FunctionalStyleProgramming
  6. {
  7. private static void PrintList(IEnumerable<int> list)
  8. {
  9. foreach(int elem in list){
  10. Console.Write(elem);
  11. Console.Write(' ');
  12. }
  13. Console.WriteLine();
  14. }
  15.  
  16. /// Problem: You need to get a list that contains even numbers
  17. /// ranged from 0 up to 100
  18. ///
  19. public static void Main()
  20. {
  21. // First, using the traditional for-loop
  22. var even_numbers_for = new List<int>();
  23. even_numbers_for.Capacity = 100;
  24. for(int i = 0; i < 100; ++i){
  25. if(i % 2 == 0)
  26. even_numbers_for.Add(i);
  27. }
  28.  
  29. // Second, using the slightly sophisticated foreach-loop
  30. var even_numbers_foreach = new List<int>();
  31. even_numbers_foreach.Capacity = 100;
  32. foreach(int j in Enumerable.Range(0, 100)){
  33. if(j % 2 == 0)
  34. even_numbers_foreach.Add(j);
  35. }
  36.  
  37. // Third, using LINQ to Objects
  38. var even_numbers_lto =
  39. Enumerable.Range(0, 100)
  40. .Where(n => n % 2 == 0);
  41.  
  42. // And lastly using LINQ to SQL(which I strongly recommend to use, and is familiar to those who are used to SQL)
  43. var even_numbers_lts =
  44. from n in Enumerable.Range(0, 100)
  45. where n % 2 == 0
  46. select n;
  47. //Darn Ideone! It doesn't hightlight LINQ to SQL syntaxes! I'm very disappointed at that ;-(
  48.  
  49. PrintList(even_numbers_for);
  50. PrintList(even_numbers_foreach);
  51. PrintList(even_numbers_lto);
  52. PrintList(even_numbers_lts);
  53. // And then think about the pros and cons of each solution.
  54. // Which one do you think is the best solution?
  55. //
  56. // Extra...
  57. Note();
  58. }
  59.  
  60. private static void Note()
  61. {
  62. var first_even_number =
  63. (from n in Enumerable.Range(1, 100)
  64. where n % 2 == 0
  65. select n
  66. ).FirstOrDefault();
  67.  
  68. Console.WriteLine("The first even number above 0 is {0}.", first_even_number);
  69.  
  70. var recommended =
  71. from n in Enumerable.Range(1, 100)
  72. where n % 2 == 0
  73. select n;
  74.  
  75. var recommended_first_even_number = recommended.FirstOrDefault();
  76. Console.WriteLine("The first even number above 0 is {0}.", recommended_first_even_number);
  77. }
  78. }
Success #stdin #stdout 0.04s 34776KB
stdin
Standard input is empty
stdout
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 
The first even number above 0 is 2.
The first even number above 0 is 2.