Safely get the value of the given property as an array of strings, or return an empty array if no value is present in the dictionary. This assumes the property is stored as a comma-separated list - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

Safely get the value of the given property as an array of strings, or return an empty array if no value is present in the dictionary. This assumes the property is stored as a comma-separated list

Demo Code

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

public class Main{
        /// <summary>
        /// Safely get the value of the given property as an array of strings, or return an empty array if no value is present in the dictionary.
        /// This assumes the property is stored as a comma-separated list
        /// </summary>
        /// <typeparam name="TKey">The disctionary key 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 as a string array, or the default if no value is specified</returns>
        public static string[] GetPropertyAsArray<TKey>(this IDictionary<TKey, string> dictionary, TKey property)
        {
            if (dictionary.ContainsKey(property))
            {
                return dictionary[property].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            }

            return new string[0];
        }
}

Related Tutorials