Get Or Add from IDictionary - CSharp System.Collections

CSharp examples for System.Collections:IDictionary

Description

Get Or Add from IDictionary

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from   w ww .  j  a  va2  s.  c  o  m*/

public class Main{
        public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> source, TKey key, Func<TKey, TValue> createValue)
        {
            TValue value;
            if (!source.TryGetValue(key, out value))
            {
                source[key] = value = createValue(key);
            }

            return value;
        }
}

Related Tutorials