Listnumbers = new List { 1, 2, 3, 4, 5 }; int index = numbers.IndexOf(3); Console.WriteLine(index); // Output: 2
ListIn this example, a list of strings is created and the IndexOf() method is used to find the index of the value "pear". Since "pear" is not in the list, the output will be -1. The System.Collections.Generic namespace is the package library that contains the List class and other generic collection classes.fruits = new List { "apple", "banana", "orange" }; int index = fruits.IndexOf("pear"); Console.WriteLine(index); // Output: -1