Skip to content

Instantly share code, notes, and snippets.

@marcelschmidtdev
Last active November 18, 2023 17:23
Show Gist options
  • Save marcelschmidtdev/627ef361f556c5caf722459c7fae6f98 to your computer and use it in GitHub Desktop.
Save marcelschmidtdev/627ef361f556c5caf722459c7fae6f98 to your computer and use it in GitHub Desktop.
Elegant way to get the next element in an array or list (circled)
//Assume we have 5 items in our list
List<Item> myList = new List<Item>(5);
//Assume our current active item is at index 2
public Item GetNextItem(Item item)
{
int currentIndex = myList.IndexOf(item);
int nextItemIndex = (currentIndex + 1) % myList.Count;
return myList[nextItemIndex];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment