EN
C# / .NET - get index of List element
0 points
In this article, we would like to show you how to get index of List element in C#.
Quick solution:
xxxxxxxxxx
1
List<string> myList = new List<string>();
2
3
myList.IndexOf("value");
In this example, we want to get the index of an item with Banana
value from fruits
ArrayList.
xxxxxxxxxx
1
using System;
2
using System.Collections.Generic;
3
4
public class Program
5
{
6
public static void Main()
7
{
8
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
9
10
int result = fruits.IndexOf("Banana");
11
12
Console.WriteLine(result);
13
}
14
}
Output:
xxxxxxxxxx
1
1
Note:
If there are multiple items with specified value, the
IndexOf()
method will return index of the first one.