C# program to get the last element from an array


Firstly, set an array −

string[] str = new string[]{
   "Java",
   "HTML",
   "jQuery",
   "JavaScript",
   "Bootstrap"
};

To get the value of the last element, get the length and display the following value −

str[str.Length - 1]

The above returns the last element.

Here is the complete code −

Example

 Live Demo

using System;

public class Demo {
   public static void Main() {
      string[] str = new string[] {
         "Java",
         "HTML",
         "jQuery",
         "JavaScript",
         "Bootstrap"
      };
      Console.WriteLine("Array...");
      foreach(string res in str) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Last element: "+str[str.Length - 1]);
   }
}

Output

Array...
Java
HTML
jQuery
JavaScript
Bootstrap
Last element: Bootstrap

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements