Omar Omar - 2 months ago 5x
C# Question

Challenge: C# Foreach - Before, After, Even, Odd, Last, First

Since C# doesn't have a before,after,last,first etc. as part of its foreach. The challenge is to mimic this behavior as elegantly as possible with the following criteria:

  1. Must allow: before, first, even, odd, last, after events

  2. Give an option execute/not execute the primary function (function executed on all objects of the collection) during the events listed in #1

If you can exceed the above criteria, pleases do!

I'll post my answer below, but its not elegant nor is it feasible, so I would like to see what the community can conjure up.

hard coded for loops get annoying sometimes =(



  • after: .SkipWhile(predicate) (left vague as your meaning isn't clear)
  • before: .TakeWhile(predicate) (left vague as your meaning isn't clear)
  • last: .Last()
  • first: .First()
  • odd: .Where((x,i)=>i%2==1)
  • even: .Where((x,i)=>i%2==0)