How to get an index from the start and from end of an array in C#

1 Answer

0 votes
using System;

public class Program
{
    public static void Main(string[] args)
    {
        int[] array = { 12, 234, 0, 94, 17, 98,  120, 773, 8372, 4, 6};

        Index i1 = new Index(3); // from start
        Index i2 = new Index(3, fromEnd: true); // from end

        int index1 = i1.GetOffset(array.Length);
        int index2 = i2.GetOffset(array.Length);

        Console.WriteLine(array[index1]); 
        Console.WriteLine(array[index2]); 
    }
}



/*
run:

94
8372

*/

 



answered Mar 26, 2024 by avibootz
...