/// <summary> /// Returns a generic <see cref="IReadOnlyList{T}"/> /// of <see cref="ValueType"/> values parsed /// from a collection of strings using a specified /// <see cref="TryParseFromString{T}"/> delegate, /// ignoring any values which cannot be parsed /// and optionally having duplicates removed. /// </summary> /// <typeparam name="T"> /// The type of array to return, must /// be a <see cref="ValueType"/>. /// </typeparam> /// <param name="strings"> /// A collection of strings to be converted /// to <typeparamref name="T"/>. /// </param> /// <param name="tryParseDelegate"> /// A delegate method conforming to the /// <see cref="TryParseFromString{T}"/> /// signature, for optimized type conversion /// via a "TryParse" method common to many /// primitive <see cref="ValueType"/> classes. /// </param> /// <param name="removeDuplicates"> /// Whether to remove duplicate items in the array returned. /// Parameter is optional, default value is <c>false</c>. /// </param> /// <returns> /// A list of type <typeparamref name="T"/>. /// </returns> public static IReadOnlyList <T> ToEnumerable <T>( this IEnumerable <string> strings, TryParseFromString <T> tryParseDelegate, bool removeDuplicates = ConvertStructCollection.DefaultRemoveDuplicates) where T : struct { return(strings.ToList( tryParseDelegate, removeDuplicates)); }
/// <summary> /// Returns a generic <see cref="List{T}"/> /// of <see cref="ValueType"/> values parsed /// from a collection of strings using a specified /// <see cref="TryParseFromString{T}"/> delegate, /// ignoring any values which cannot be parsed /// and optionally having duplicates removed. /// </summary> /// <typeparam name="T"> /// The type of array to return, must /// be a <see cref="ValueType"/>. /// </typeparam> /// <param name="strings"> /// A collection of strings to be converted /// to <typeparamref name="T"/>. /// </param> /// <param name="tryParseDelegate"> /// A delegate method conforming to the /// <see cref="TryParseFromString{T}"/> /// signature, for optimized type conversion /// via a "TryParse" method common to many /// primitive <see cref="ValueType"/> classes. /// </param> /// <param name="removeDuplicates"> /// Whether to remove duplicate items in the array returned. /// Parameter is optional, default value is <c>false</c>. /// </param> /// <returns> /// A list of type <typeparamref name="T"/>. /// </returns> public static List <T> ToList <T>( this IEnumerable <string> strings, TryParseFromString <T> tryParseDelegate, bool removeDuplicates = ConvertStructCollection.DefaultRemoveDuplicates) where T : struct { if (tryParseDelegate == null) { return(strings.ToList <T>(null, false, removeDuplicates)); } List <T> list = new List <T>(); if (strings == null) { return(list); } // Do not try to parse strings that are null or white space. foreach (string s in strings.Where(value => !string.IsNullOrWhiteSpace(value))) { T result; if (!tryParseDelegate(s, out result)) { continue; } if (removeDuplicates && list.Contains(result)) { continue; } list.Add(result); } return(list); }