code_junkie

foreach with generic List, detecting first iteration when using value type

Nov 13th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. List<object> objs = new List<object>
  2. {
  3. new Object(),
  4. new Object(),
  5. new Object(),
  6. new Object()
  7. };
  8.  
  9. foreach (object o in objs)
  10. {
  11. if (o == objs.First())
  12. {
  13. System.Diagnostics.Debug.WriteLine("First object - do something special");
  14. }
  15. else
  16. {
  17. System.Diagnostics.Debug.WriteLine("object Do something else");
  18. }
  19. }
  20.  
  21. List<int> ints = new List<int> { 0, 0, 0, 0 };
  22. foreach (int i in ints)
  23. {
  24. if (i == ints.First())
  25. {
  26. System.Diagnostics.Debug.WriteLine("First int - do something special");
  27. }
  28. else
  29. {
  30. System.Diagnostics.Debug.WriteLine("int Do something else");
  31. }
  32. }
  33.  
  34. List<int> ints = new List<int> { 0, 0, 0, 0 };
  35. foreach (int i in ints)
  36. {
  37. if (i == ints.First())
  38. {
  39. System.Diagnostics.Debug.WriteLine("First int - do something special");
  40. }
  41. else
  42. {
  43. System.Diagnostics.Debug.WriteLine("int Do something else");
  44. }
  45. }
  46.  
  47. boolean
  48.  
  49. using(var iter = ints.GetEnumerator()) {
  50. if(iter.MoveNext()) {
  51. // do "first" with iter.Current
  52.  
  53. while(iter.MoveNext()) {
  54. // do something with the rest of the data with iter.Current
  55. }
  56. }
  57. }
  58.  
  59. if(ints.Any()) {
  60. var first = ints.First();
  61. // do something with first
  62. }
  63.  
  64. foreach(var item in ints.Skip(1)) {
  65. // do something with the rest of them
  66. }
  67.  
  68. using System;
  69. using System.Collections.Generic;
  70.  
  71. using MiscUtil.Collections;
  72.  
  73. class Example
  74. {
  75. static void Main(string[] args)
  76. {
  77. List<string> list = new List<string>();
  78. list.Add("a");
  79. list.Add("b");
  80. list.Add("c");
  81. list.Add("d");
  82. list.Add("e");
  83.  
  84. foreach (SmartEnumerable<string>.Entry entry in
  85. new SmartEnumerable<string>(list))
  86. {
  87. Console.WriteLine ("{0,-7} {1} ({2}) {3}",
  88. entry.IsLast ? "Last ->" : "",
  89. entry.Value,
  90. entry.Index,
  91. entry.IsFirst ? "<- First" : "");
  92. }
  93. }
  94. }
  95.  
  96. foreach(int i in ints.Take(1))
  97. {
  98. //do first thing
  99. }
  100.  
  101. foreach(int i in ints.Skip(1))
  102. {
  103. //do other things
  104. }
  105.  
  106. var list = new List<Int32>();
  107.  
  108. // do something with list.First();
  109.  
  110. foreach (var item in list.Skip(1))
  111. {
  112. // do other stuff
  113. }
  114.  
  115. List<int> ints = new List<int> { 0, 0, 0, 0 };
  116.  
  117. System.Diagnostics.Debug.WriteLine("First int - do something special" + ints.FirstOrDefault().ToString());
  118. foreach (int i in ints.Skip(1))
  119. {
  120. {
  121. System.Diagnostics.Debug.WriteLine("int Do something else");
  122. }
  123. }
Add Comment
Please, Sign In to add comment