Last active
November 18, 2023 17:23
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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