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#?
Approach with ElementOrDefault()
ElementOrDefault()
https://dotnetfiddle.net/fxVo6T
int?[] items = { 1, 3, 5, 7, 9, 13 }; for (int i = 0; i < items.Length; i++) { int? previous = items.ElementAtOrDefault(i - 1); int? current = items.ElementAtOrDefault(i); int? next = items.ElementAtOrDefault(i + 1); }