List:
List<int> list1 = new List<int>(){ 0, 1, 2, 3, 4, 5, 6 };
let’s say we want to reorder it. The beginning should be at number “2”
// 2,3,4,5,6,0,1
or at number 5
// 5,6,0,1,2,3,4
how do you do it with C#?
the reason: Imagine that you have an index of a given number in the List (number 3, index 3). You want to get the second number from the right – it’ll be 5.
Unfortunately, if the starting number is at the end of the List (numbers 5 and 6) – out of range exception will be thrown, because there’s no 7 and 8!
The idea is to reorder the List!
- We enter Nr. 5 – we get 0 (5,6,0).
- We enter Nr. 6 – we get 1 (6,0,1), etc.
or maybe there is some other (read – better) way to solve this problem?
You don’t need to reorder the list. You could get the number with the following function:
You could call the function like this: