Matches value with its index in the array in CSharp

Description

The following code shows how to matches value with its index in the array.

Example


  // w w  w.j a v a 2s. c  o m
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {

        int[] numbers = { 5, 2, 1, 3, 9};

        var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) });

        foreach (var n in numsInPlace) {
            Console.WriteLine("{0}: {1}", n.Num, n.InPlace);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ