Safely get the value of the given property, or return the default if no value is present in the dictionary - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

Safely get the value of the given property, or return the default if no value is present in the dictionary

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License");
using System.Linq;
using System.Collections.Generic;
using System;//from  w w  w.  j a v  a  2 s  . c  o m

public class Main{
        /// <summary>
        /// Safely get the value of the given property, or return the default if no value is present in the dictionary
        /// </summary>
        /// <typeparam name="TKey">The disctionary key type</typeparam>
        /// <typeparam name="TValue">The dictionary value type</typeparam>
        /// <param name="dictionary">The extensions dictionary to search</param>
        /// <param name="property">The property to serach for</param>
        /// <returns>The value stored in the dictionary, or the default if no value is specified</returns>
        public static TValue GetProperty<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey property)
        {
            if (dictionary.ContainsKey(property))
            {
                return dictionary[property];
            }

            return default(TValue);
        }
}

Related Tutorials