Get Or Default from IReadOnlyDictionary - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

Get Or Default from IReadOnlyDictionary

Demo Code


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

public class Main{
        public static TValue GetOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source, TKey key)
        {
            TValue value;
            if (source.TryGetValue(key, out value))
            {
                return value;
            }

            return default(TValue);
        }
}

Related Tutorials