fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. Print(new[] { 1, 2, 3, 4 });
  10. Print(new[] { "hello", "world" });
  11. Print(new[] { "just one!" });
  12. }
  13.  
  14. public static void Print<T>(IEnumerable<T> collection) {
  15. if (collection.Any()) {
  16. var seenFirst = false;
  17. T prev = default(T);
  18. foreach (var current in collection) {
  19. if (seenFirst) Foo(prev);
  20. seenFirst = true;
  21. prev = current;
  22. }
  23. Bar(prev);
  24. }
  25. }
  26.  
  27. public static void Foo(object o) { Console.WriteLine("Foo: {0}", o); }
  28. public static void Bar(object o) { Console.WriteLine("Bar: {0}", o); }
  29. }
  30.  
Success #stdin #stdout 0.04s 38080KB
stdin
Standard input is empty
stdout
Foo: 1
Foo: 2
Foo: 3
Bar: 4
Foo: hello
Bar: world
Bar: just one!