C# List.IndexOf() strategy: Here, we will find out about the IndexOf() technique for List with example.
C# List.IndexOf() Method
List.IndexOf() technique is utilized to get the file of first event of a component in the rundown.
Syntax:
int List<T>.IndexOf(T item);
int List<T>.IndexOf(T item, int start_index);
int List<T>.IndexOf(T item, int start_index, int count);
Parameter: the thing is a component of type T, whose first event will be returned if thing found.
- start_index is the beginning situation from where you need to discover the component in the rundown.
- tally is the all-out components from “start_index” to look through the component in the rundown.
- Return value: It returns record of the component if the component establishes in the rundown between indicated lists if the component doesn’t found in the rundown – it returns – 1.
Example:
int list declaration:
List<int> a = new List<int>();
adding elements:
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
Method calls:
a.IndexOf(20) //output: 1
a.IndexOf(100) //output: -1
a.IndexOf(20, 1) //output: 1
a.IndexOf(100, 1) //output: -1
a.IndexOf(20, 1, 3) //output: 1
a.IndexOf(20, 0, 1) //output: -1
C# Example to get the record a component in the rundown utilizing List.IndexOf() Method:
using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void printList(List<int> lst)
{
//printing elements
foreach (int item in lst)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
//integer list
List<int> a = new List<int>();
//adding elements
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
//print the list
Console.WriteLine("list elements...");
printList(a);
//using List.IndexOf(T item)
//finding 20
int index = a.IndexOf(20);
if (index != -1)
Console.WriteLine("20 found at " + index + " position.");
else
Console.WriteLine("20 does not found in the list");
//finding 100
index = a.IndexOf(100);
if (index != -1)
Console.WriteLine("100 found at " + index + " position.");
else
Console.WriteLine("100 does not found in the list");
//using List.IndexOf(T item, int index)
//finding 20
index = a.IndexOf(20, 1); //start index is 1
if (index != -1)
Console.WriteLine("20 found at " + index + " position.");
else
Console.WriteLine("20 does not found in the list");
//finding 100
index = a.IndexOf(100, 1); //start index is 1
if (index != -1)
Console.WriteLine("100 found at " + index + " position.");
else
Console.WriteLine("100 does not found in the list");
//using List.IndexOf(T item, int start_index, int count)
//finding 20
//search will perform from index 1 to next 3 elements
index = a.IndexOf(20, 1, 3);
if (index != -1)
Console.WriteLine("20 found at " + index + " position.");
else
Console.WriteLine("20 does not found in the list");
//finding 20
//search will perform from index 0 to next 1 element
index = a.IndexOf(20, 0, 1);
if (index != -1)
Console.WriteLine("20 found at " + index + " position.");
else
Console.WriteLine("20 does not found in the list");
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output:
list elements...
10 20 30 40 50 10 20 30 40 50
20 found at 1 position.
100 does not found in the list
20 found at 1 position.
100 does not found in the list
20 found at 1 position.
20 does not found in the list