Implementation of Scala's Zip With Index method. Folds a collection into a Dictionary where the original value (of type T) acts as the key and the index of the item in the array acts as the value. - CSharp System

CSharp examples for System:Array Index

Description

Implementation of Scala's Zip With Index method. Folds a collection into a Dictionary where the original value (of type T) acts as the key and the index of the item in the array acts as the value.

Demo Code

//     Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
using System.Linq;
using System.Collections.Generic;
using System;// w  ww .  j  ava2 s. com

public class Main{
        /// <summary>
        /// Implementation of Scala's ZipWithIndex method.
        /// 
        /// Folds a collection into a Dictionary where the original value (of type T) acts as the key
        /// and the index of the item in the array acts as the value.
        /// </summary>
        public static Dictionary<T, int> ZipWithIndex<T>(this IEnumerable<T> collection)
        {
            var i = 0;
            var dict = new Dictionary<T, int>();
            foreach (var item in collection)
            {
                dict.Add(item, i);
                i++;
            }
            return dict;
        }
}

Related Tutorials