Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to display the first element from an array
In C#, there are multiple ways to access the first element from an array. The most common approaches include using array indexing, the First() LINQ method, and the Take() method.
Syntax
Following is the syntax for accessing the first element using array indexing −
dataType firstElement = arrayName[0];
Following is the syntax using the LINQ First() method −
dataType firstElement = arrayName.First();
Using Array Indexing
The simplest and most efficient way to get the first element is using zero-based indexing −
using System;
class Demo {
static void Main() {
double[] myArr = {20.5, 35.6, 45.7, 55.6, 79.7};
double firstElement = myArr[0];
Console.WriteLine("First element: " + firstElement);
}
}
The output of the above code is −
First element: 20.5
Using LINQ First() Method
The First() method from LINQ provides a more functional approach to retrieve the first element −
using System;
using System.Linq;
class Demo {
static void Main() {
double[] myArr = {20.5, 35.6, 45.7, 55.6, 79.7};
double res = myArr.First();
Console.WriteLine("First element using LINQ: " + res);
}
}
The output of the above code is −
First element using LINQ: 20.5
Using Take() Method
The Take() method can be used to get the first element by taking only one element from the array −
using System;
using System.Linq;
class Demo {
static void Main() {
string[] names = {"Alice", "Bob", "Charlie", "David"};
string firstName = names.Take(1).First();
Console.WriteLine("First name: " + firstName);
}
}
The output of the above code is −
First name: Alice
Comparison of Approaches
| Method | Performance | Exception Handling | Use Case |
|---|---|---|---|
| Array Indexing [0] | Fastest | IndexOutOfRangeException for empty arrays | Best for direct access |
| LINQ First() | Slower | InvalidOperationException for empty collections | Good for functional programming style |
| Take(1).First() | Slowest | InvalidOperationException for empty collections | When working with IEnumerable operations |
Handling Empty Arrays
When working with potentially empty arrays, use FirstOrDefault() to avoid exceptions −
using System;
using System.Linq;
class Demo {
static void Main() {
int[] emptyArray = {};
int[] normalArray = {10, 20, 30};
int defaultValue = emptyArray.FirstOrDefault();
int firstValue = normalArray.FirstOrDefault();
Console.WriteLine("Empty array first element: " + defaultValue);
Console.WriteLine("Normal array first element: " + firstValue);
}
}
The output of the above code is −
Empty array first element: 0 Normal array first element: 10
Conclusion
Array indexing with [0] is the most efficient way to access the first element, while LINQ methods like First() and FirstOrDefault() provide more safety and functional programming style. Choose the method based on your performance requirements and error handling needs.
