CSharp examples for System.Collections.Generic:IDictionary
Given a collection of values a key function, builds a dictionary
/*/*from w w w . ja v a 2s .c o m*/ * ==================== * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of the Common Development * and Distribution License("CDDL") (the "License"). You may not use this file * except in compliance with the License. * * You can obtain a copy of the License at * http://opensource.org/licenses/cddl1.php * See the License for the specific language governing permissions and limitations * under the License. * * When distributing the Covered Code, include this CDDL Header Notice in each file * and include the License file at http://opensource.org/licenses/cddl1.php. * If applicable, add the following below this CDDL Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * ==================== */ using System.Text; using System.Collections.Generic; using System.Reflection; using System.Runtime.CompilerServices; using System; public class Main{ /// <summary> /// Given a collection of values a key function, builds a dictionary /// </summary> /// <param name="values">List of values</param> /// <param name="keyFunction">Key function, mapping from key to value</param> /// <returns>The dictionay</returns> public static IDictionary<TKey2, TValue2> NewDictionary<TKey1, TValue1, TKey2, TValue2>( IDictionary<TKey1, TValue1> original) { IDictionary<TKey2, TValue2> rv = new Dictionary<TKey2, TValue2>(); if (original != null) { foreach (KeyValuePair<TKey1, TValue1> entry in original) { //DONT use Add - it throws exceptions if already there rv[(TKey2)(object)entry.Key] = (TValue2)(object)entry.Value; } } return rv; } /// <summary> /// Given a collection of values a key function, builds a dictionary /// </summary> /// <param name="values">List of values</param> /// <param name="keyFunction">Key function, mapping from key to value</param> /// <returns>The dictionay</returns> public static IDictionary<TKey, TValue> NewDictionary<TKey, TValue>( IDictionary<TKey, TValue> original) { return NewDictionary<TKey, TValue, TKey, TValue>(original); } /// <summary> /// Given a collection of values a key function, builds a dictionary /// </summary> /// <param name="values">List of values</param> /// <param name="keyFunction">Key function, mapping from key to value</param> /// <returns>The dictionay</returns> public static IDictionary<TKey, TValue> NewDictionary<TKey, TValue>( IEnumerable<TValue> values, KeyFunction<TKey, TValue> keyFunction) { IDictionary<TKey, TValue> rv = new Dictionary<TKey, TValue>(); if (values != null) { foreach (TValue value in values) { TKey key = keyFunction(value); //DONT use Add - it throws exceptions if already there rv[key] = value; } } return rv; } public static IDictionary<T, K> NewDictionary<T, K>(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3, T k4, K v4, T k5, K v5) { IDictionary<T, K> map = NewDictionary(k0, v0, k1, v1, k2, v2, k3, v3, k4, v4); map[k5] = v5; return map; } public static IDictionary<T, K> NewDictionary<T, K>(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3, T k4, K v4) { IDictionary<T, K> map = NewDictionary(k0, v0, k1, v1, k2, v2, k3, v3); map[k4] = v4; return map; } public static IDictionary<T, K> NewDictionary<T, K>(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3) { IDictionary<T, K> map = NewDictionary(k0, v0, k1, v1, k2, v2); map[k3] = v3; return map; } public static IDictionary<T, K> NewDictionary<T, K>(T k0, K v0, T k1, K v1, T k2, K v2) { IDictionary<T, K> map = NewDictionary(k0, v0, k1, v1); map[k2] = v2; return map; } public static IDictionary<T, K> NewDictionary<T, K>(T k0, K v0, T k1, K v1) { IDictionary<T, K> map = NewDictionary(k0, v0); map[k1] = v1; return map; } /// <summary> /// Given a collection of values a key function, builds a dictionary /// </summary> /// <param name="k0">List of values</param> /// <param name="v0">Key function, mapping from key to value</param> /// <returns>The dictionay</returns> public static IDictionary<TKey, TValue> NewDictionary<TKey, TValue>( TKey k0, TValue v0) { IDictionary<TKey, TValue> rv = new Dictionary<TKey, TValue>(); rv[k0] = v0; return rv; } }