Says I have this List : 1, 3, 5, 7, 9, 13
For example, given value is : 9, the previous item is 7 and the next item is 13
How can I achieve this using C#?
Also if you want compact solution with circular logic without creating new list you can use following code:
int nextNumber = list[(list.IndexOf(currentNumber) + 1) % list.Count]; int previousNumber = list[(list.IndexOf(currentNumber) - 1 + list.Count) % list.Count];
https://dotnetfiddle.net/PkP2Jy