C# Tutorial - Array Index Position
C# Array.IndexOf, LastIndexOf: Search Arrays - Dot Net Perls



Part 4. write the program to find the array index position .


public class Program

    {
        static void Main(string[] args)
        {
            //initialize  the array
            int[] arr = { 1, 4, 2, 5, 6 };

            //find the specfic index postion

            int val = Array.IndexOf(arr, 5);
            Console.WriteLine("Index Postion is : "+val);        
            Console.ReadLine();
        }

    }


Index Postion is : 3


Using LINQ to find specific index position pair up values and index.

 public class Program

    {
        static void Main(string[] args)
        {
            //initializing the array
            int[] arr = { 1, 4, 2, 5, 6 };

            //find the specific index position Pair up values and indexes Do the filtering       
         
  var data = arr.Select((v, i) => new { Index = i, Value = v }) 
   .Where(p => p.Value == 5).Select(p => p.Index).ToList();

           Console.WriteLine("Index Postion is : "+data[0]);        
            Console.ReadLine();
        }

    }



Index Postion is : 3




Thanks for reading this article, hope you enjoyed it.If any Query drop a comment.