Get Dictionary Converter - CSharp System.Collections

CSharp examples for System.Collections:IDictionary

Description

Get Dictionary Converter

Demo Code


using System.Collections.Generic;
using System.Collections;
using System;//from   ww  w . j a va  2s  .co  m

public class Main{
        internal static Converter<TKey, TValue> GetDictionaryConverter<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TValue defaultValue)
        {
            if (dictionary == null)
                throw new ArgumentNullException("dictionary");

            return delegate(TKey key) {
                TValue value;
                if (dictionary.TryGetValue(key, out value))
                    return value;
                else
                    return defaultValue;
            };
        }
        internal static Converter<TKey, TValue> GetDictionaryConverter<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
        {
            return GetDictionaryConverter(dictionary, default(TValue));
        }
}

Related Tutorials