466

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#?

2
  • 4
    See stackoverflow.com/questions/5326874/… for some discussion as to whether to use the [] operator or the ElementAt() method.
    – ngm
    Commented Sep 23, 2015 at 9:41
  • 1
    @user1909486: you haven't accepted any answers to your questions Commented Dec 8, 2020 at 1:12

6 Answers 6

513
list1[0];

Assuming list's type has an indexer defined.

3
  • 6
    The only answer that explains why it's possible to access a list, which is an object, like an array
    – PrashanD
    Commented Apr 26, 2017 at 7:10
  • 1
    Does it return a copy of the object in the list? obj = list[0]; list.clear(); // will obj still be populated? Commented Mar 3, 2020 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. Commented Mar 4, 2020 at 1:17
319

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
7
  • 36
    Just to note that ElementAt() requires using System.Linq;.
    – ngm
    Commented Sep 23, 2015 at 9:42
  • 35
    Is there any advantage to this over the standard myList[0] notation? Commented Mar 30, 2017 at 20:26
  • 14
    "Why would I use Enumerable.ElementAt() versus the [] operator?" stackoverflow.com/questions/5326874/…
    – Lavande
    Commented Jan 11, 2018 at 3:22
  • 6
    .ElementAtOrDefault();
    – amit jha
    Commented Feb 6, 2019 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 Commented Feb 21, 2020 at 15:26
45

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.

27

.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.

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?

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.