455

I've recently started using c# moving over from Java. I can't seem to find how to get a list item by index. In java to get the first item of the list it would be:

list1.get(0);

What is the equivalent in c#?

Jeff
  • 739
  • 12
  • 31
user1909486
  • 4,599
  • 2
  • 14
  • 4
  • 4
    See http://stackoverflow.com/questions/5326874/why-would-i-use-enumerable-elementat-versus-the-operator for some discussion as to whether to use the [] operator or the ElementAt() method. – ngm Sep 23 '15 at 09:41
  • 1
    @user1909486: you haven't accepted any answers to your questions – Mitch Wheat Dec 08 '20 at 01:12

6 Answers6

504
list1[0];

Assuming list's type has an indexer defined.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 6
    The only answer that explains why it's possible to access a list, which is an object, like an array – PrashanD Apr 26 '17 at 07:10
  • 1
    Does it return a copy of the object in the list? obj = list[0]; list.clear(); // will obj still be populated? – Paul McCarthy Mar 03 '20 at 14:20
  • 4
    @Paul McCarthy: you could easily test this yourself, but Yes. A list hold references to objects. Clearing the list does not affect the objects held in it. If there are no other references to those objects, they will be garbage collected at some point in time. – Mitch Wheat Mar 04 '20 at 01:17
310

You can use the ElementAt extension method on the list.

For example:

// Get the first item from the list

using System.Linq;

var myList = new List<string>{ "Yes", "No", "Maybe"};
var firstItem = myList.ElementAt(0);

// Do something with firstItem
MetaColon
  • 2,895
  • 3
  • 16
  • 38
user3004826
  • 3,179
  • 1
  • 12
  • 8
  • 36
    Just to note that ElementAt() requires `using System.Linq;`. – ngm Sep 23 '15 at 09:42
  • 34
    Is there any advantage to this over the standard `myList[0]` notation? – AnalogWeapon Mar 30 '17 at 20:26
  • 14
    "Why would I use Enumerable.ElementAt() versus the [] operator?" https://stackoverflow.com/questions/5326874/why-would-i-use-enumerable-elementat-versus-the-operator – Lavande Jan 11 '18 at 03:22
  • 6
    `.ElementAtOrDefault();` – amit jha Feb 06 '19 at 12:57
  • 6
    @AnalogWeapon yes there is - you can use it as part of a null safe chain, e.g. `myCollectionWhichMightBeNull?.ElementAt(0)` or to safeguard againt a non-existent element: `myCollection.ElementAtOrDefault(0)?.SomeProperty` – Chris Peacock Feb 21 '20 at 15:26
  • Can you pass a integer constant to either [] or ElementAt? Rather than literal numbers? – Andrew Truckle Jan 15 '22 at 18:19
  • @Lavande: another advantage is you can use ElementAt() for .Net collection types that don't support []. – paulsm4 May 01 '23 at 21:43
43

Visual Basic, C#, and C++ all have syntax for accessing the Item property without using its name. Instead, the variable containing the List is used as if it were an array:

List[index]

See, for instance, List.Item[Int32] Property.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Zeyad Qunees
  • 589
  • 5
  • 4
26

.NET List data structure is an Array in a "mutable shell".

So you can use indexes for accessing to it's elements like:

var firstElement = myList[0];
var secondElement = myList[1];

Starting with C# 8.0 you can use Index and Range classes for accessing elements. They provides accessing from the end of sequence or just access a specific part of sequence:

var lastElement = myList[^1]; // Using Index
var fiveElements = myList[2..7]; // Using Range, note that 7 is exclusive

You can combine indexes and ranges together:

var elementsFromThirdToEnd = myList[2..^0]; // Index and Range together

Also you can use LINQ ElementAt method but for 99% of cases this is really not necessary and just slow performance solution.

picolino
  • 4,856
  • 1
  • 18
  • 31
21

Old question, but I see that this thread was fairly recently active, so I'll go ahead and throw in my two cents:

Pretty much exactly what Mitch said. Assuming proper indexing, you can just go ahead and use square bracket notation as if you were accessing an array. In addition to using the numeric index, though, if your members have specific names, you can often do kind of a simultaneous search/access by typing something like:

var temp = list1["DesiredMember"];

The more you know, right?

Xellarant
  • 211
  • 2
  • 2
18

you can use index to access list elements

List<string> list1 = new List<string>();
list1[0] //for getting the first element of the list
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81