Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 3
    Good choice. One thing to note, though - Enumerable.Range has arguments int start and int count. Your examples wont work right the way they were written. You write it as if the second argument is int end. For example - Enumerable.Range(11,20) would result in 20 numbers starting with 11, and not numbers from 11 to 20. Commented Aug 2, 2012 at 22:46
  • although, if working with an Enum, why not something like? if(Enumerable.Range(MyEnum.A, MyEnum.M){ DoThing(); } else if(Enumerable.Range(MyEnum.N, MyEnum.Z){ DoAnotherThing(); } Commented Mar 6, 2014 at 14:22
  • 4
    Note that Enumerable.Range(11,20).Contains(c) is equivalent to for(int i = 11; i < 21; ++i){ if (i == c) return true; } return false; If you had a large range it would take a long time, while just using > and < would be quick and constant-time. Commented Feb 26, 2016 at 14:20
  • An improvement: Having MySwitchWithEnumerable return void is weak design for this situation. REASON: You've converted an if-else to a series of independent statements - that hides the intent, which is that they are mutually-exclusive - only one action is executed. Instead return bool, with body if (..) { action(); return true; } else return false; The calling site then shows the intention: if (MySwitchWithEnumerable(..)) else (MySwitchWithEnumerable(..));. This is preferable. However, it is also no longer a significant improvement over your original version, for this simple case. Commented Jan 17, 2017 at 23:24