MapIndexed

Builds a new collection whose elements are the results of applying the given mapping function to each of the elements of the collection.

The integer index passed to the function indicates the index (from 0) of element being transformed.

Parameters

Returns

Func<int, T, TResult> mapping

IEnumerable<T> source

IEnumerable<TResult>

Usage

Multiplying elements by its respectively index

//IEnumerable<int> source = { 1, 2, 3 }

IEnumerable<int> result = 
    source.MapIndexed(
        (index, value) => value * index);

//result = { 0, 2, 6 }

Creating a tuple with index and value

//IEnumerable<int> source = { 1, 2, 3 }

IEnumerable<int> result = 
    source.MapIndexed(
        (index, value) => (index, value) );

//result = { (0,1), (1,2), (2,3) }

Last updated