If you've ever worked with lists in C#, you know that grabbing a specific item isn't always obvious. The language gives you two solid paths to pull out that initial value—one that's lightning-fast and another that reads almost like plain English. Which one you reach for depends on what you're doing.
We're going to walk through both techniques here, look at when each makes sense, and give you a practical scenario you can drop straight into your next project.
Why Arrays Start Counting at Zero
Here's something that trips up newcomers: C# counts from zero, not one. The position of the first item is always zero. The second sits at one, the third at two, and so on. That little detail matters because accessing the wrong index throws an exception and can crash your program if you're not careful.
Understanding this indexing system is foundational to working with any collection in C#. Once it clicks, everything else falls into place.
The Bracket Method: Fast and Dependable
The most direct route is using square brackets with a zero inside them. This is the lowest-level approach and carries virtually no overhead. It just works.
“`csharp
String[] colors = {"red", "green", "blue"};
String primary = colors[0];
Console.WriteLine(primary);
“`
That code dumps "red" straight to the console. The method works with any data type—integers, strings, objects, whatever your array holds. There's no extra method to call, no namespace to import, nothing to set up.
When speed matters and you're certain the array isn't empty, this is your best bet.
The LINQ Route: Readable and Flexible
If your project already references System.Linq, you have another option. The First extension method does exactly what it says—it grabs the first item. The syntax reads naturally, almost like a sentence.
“`csharp
Using System.Linq;
String[] fruits = {"apple", "banana", "cherry"};
String starter = fruits.First();
Console.WriteLine(starter);
“`
Output shows "apple" as expected. But here's where it gets interesting. First also accepts a predicate, which means you can pull the first item that matches a condition you define. Say you want the first element that satisfies a rule—greater than a threshold, starting with a certain letter, anything you can express as a boolean test. You write that right in the method call without building loops or temporary variables.
That kind of filtering would normally require a loop and a flag variable. LINQ handles it in a single expression.
Our Analysis
So which should you actually use? It depends on what you're building.
The bracket approach wins for performance. There's no method call involved, no virtual dispatch, nothing to slow things down. In tight loops processing large datasets, that adds up.
LINQ wins for readability and safety. It fails loudly if the collection is empty, which sounds bad but actually helps you catch bugs faster. And when you add filtering predicates, the code stays clean.
One thing to watch: First() throws an exception on empty arrays. If there's any chance your collection might have no items, use FirstOrDefault() instead. It returns the default value for your type—null for objects, zero for integers—without crashing.
Both approaches are effectively constant time, so the performance argument mostly disappears outside of extreme scenarios.
A Real-World Scenario
Imagine you're building a dashboard that shows the most recent item from a sorted collection. Say your data arrives sorted with the newest entry first, so the most recent is always at position zero.
“`csharp
Item[] recentItems = FetchRecentItems();
Item newest = recentItems[0];
DisplayOnDashboard(newest);
“`
One line. No iteration, no LINQ, no fuss. That's the power of understanding how indexing works in C#.
Alternatively, if you only want items matching a specific criterion, LINQ lets you combine sorting and filtering in one expression without extra variables lying around.
Bottom Line
Both approaches accomplish the same task. Your bracket index gets you there fastest with zero dependencies. LINQ's First() reads better and handles filtering elegantly. Most of the time, performance differences won't matter—pick whichever makes your code clearer. Just remember to check for empty collections if you're writing defensive code.
Key takeaways
- Arrays in C# are zero-indexed, meaning the first element sits at position zero
- Direct bracket access (array[0]) is the fastest method with no overhead
- LINQ's First() method adds readability and supports predicate-based filtering
- Always guard against empty arrays unless you're certain they contain data
- FirstOrDefault() provides a safe fallback when collections might be empty
Frequently asked questions
What happens if I use First() on an empty array?
Your program throws an InvalidOperationException. Use FirstOrDefault() instead when empty arrays are possible.
Can I use these methods on List objects too?
Yes. Both List and arrays support bracket indexing and LINQ extension methods identically.
Is there a performance difference between these two approaches?
Bracket indexing is technically faster by a negligible amount. For typical applications, the difference won't be noticeable.
What's the default value FirstOrDefault() returns?
It returns null for reference types and zero for value types like integers.
Can I access the last element the same way?
Yes, use array[array.Length – 1] or LINQ's Last() method. The same empty-collection rules apply.