Is there a better way to return the next item in a list and loop from the end to the front?

c#, enumeration, linq, list

Solution

I think maybe you can change the line

return (index + 1 == items.Count) ? items[0] : items[index + 1];

for something like

return items[(index + 1) % items.Count];

Problem

I have the following list of distinct strings: "A" "B" "C" If I want the item after A, I get B. After B, I get C. After C, I get A. Currently I have the following code, but for some reason it feels to me that there is a better way to go about this (maybe?). ``` private string GetNext(IList<string> items, string curr) { if (String.IsNullOrWhitespace(curr)) return items[0]; var index = items.IndexOf(curr); if (index == -1) return items[0]; return (index + 1 == items.Count) ? items[0] : items[index + 1]; } ``` I'm definitely open to a LINQ-esque way of doing this as well :)

Original source

Related problems