Get value from Dictionary with default value - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

Get value from Dictionary with default value

Demo Code


using System.Reflection;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/* w  w  w.  j a va  2 s.co  m*/

public class Main{
        public static V Get<K, V>(this Dictionary<K, V> dict, K key, V defaultValue = default(V))
   {
      V value;
      if (!dict.TryGetValue(key, out value))
      {
          value = defaultValue;
      }
      return value;
   }
}

Related Tutorials