How to get the first and last elements from an array in C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        string[] arr = { "c#", "c", "c++", "java", "rust" };
       
        string first = arr[0];
        string last = arr[arr.Length - 1];
        
        Console.WriteLine($"FIRST: {first}");
        Console.WriteLine($"LAST: {last}");
    }
}




/*
run:

FIRST: c#
LAST: rust

*/

 



answered Mar 5, 2023 by avibootz

Related questions

1 answer 121 views
1 answer 141 views
1 answer 129 views
1 answer 143 views
...