C# program to get the last element from an array

In C#, there are several ways to get the last element from an array. The most common approach is to use the array's Length property to access the element at the last index position.

Syntax

Following is the syntax to get the last element using array indexing −

arrayName[arrayName.Length - 1]

You can also use LINQ methods to get the last element −

arrayName.Last()
arrayName.LastOrDefault()

Using Array Indexing

The most efficient way to get the last element is by using the array's length minus one as the index −

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]);
   }
}

The output of the above code is −

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

Using LINQ Last() Method

You can use LINQ's Last() method to get the last element. This method throws an exception if the array is empty −

using System;
using System.Linq;

public class Demo {
   public static void Main() {
      int[] numbers = {10, 20, 30, 40, 50};
      
      int lastElement = numbers.Last();
      Console.WriteLine("Array: " + string.Join(", ", numbers));
      Console.WriteLine("Last element using Last(): " + lastElement);
      
      // Using LastOrDefault() for safe access
      int lastOrDefault = numbers.LastOrDefault();
      Console.WriteLine("Last element using LastOrDefault(): " + lastOrDefault);
   }
}

The output of the above code is −

Array: 10, 20, 30, 40, 50
Last element using Last(): 50
Last element using LastOrDefault(): 50

Handling Empty Arrays

When working with potentially empty arrays, it's important to check the length before accessing elements −

using System;
using System.Linq;

public class Demo {
   public static void Main() {
      string[] emptyArray = new string[0];
      string[] validArray = {"First", "Second", "Third"};
      
      // Safe method using length check
      if (validArray.Length > 0) {
         Console.WriteLine("Last element (safe): " + validArray[validArray.Length - 1]);
      }
      
      // Using LastOrDefault() returns default value for empty arrays
      string lastElement = emptyArray.LastOrDefault();
      Console.WriteLine("Last element from empty array: " + (lastElement ?? "null"));
      
      lastElement = validArray.LastOrDefault();
      Console.WriteLine("Last element from valid array: " + lastElement);
   }
}

The output of the above code is −

Last element (safe): Third
Last element from empty array: null
Last element from valid array: Third

Comparison of Methods

Method Performance Empty Array Behavior
array[array.Length - 1] Fastest - O(1) Throws IndexOutOfRangeException
array.Last() Slower - requires LINQ Throws InvalidOperationException
array.LastOrDefault() Slower - requires LINQ Returns default value (null for reference types)

Conclusion

Getting the last element from an array in C# is most efficiently done using array[array.Length - 1]. For safer operations with potentially empty arrays, use LastOrDefault() or check the array length before accessing elements to avoid exceptions.

Updated on: 2026-03-17T07:04:35+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements