Exemple #1
0
        /// <summary>
        /// Alters the contents of the array and ignores others based upon the two
        /// <see cref="Common.TranslateArgument{TResult, TArgument}"/> delegates.
        /// </summary>
        /// <typeparam name="TSourceItem">The type of the parameter accepted as input.</typeparam>
        /// <typeparam name="TDestinationItem">The type of result that is expected.</typeparam>
        /// <param name="items">The array to be logically and data-wise altered.</param>
        /// <param name="filter">The delegate that determines which elements of <paramref name="items"/> are
        /// kept in the result array.</param>
        /// <param name="translator">The delegate that alters the value of the elements in <paramref name="items"/>.</param>
        /// <returns>A new array that contains the specific items transformed as desired by the two iteration logic delegates.</returns>
        public static TDestinationItem[] TranslateFilteredArray <TSourceItem, TDestinationItem>
            (TSourceItem[] items,
            Predicate <TSourceItem> filter,
            Common.TranslateArgument <TDestinationItem, TSourceItem> translator)
        {
            //Setup space.
            List <TDestinationItem> resultItems = new List <TDestinationItem>(items.Length);

            //Iterate, filter, and translate.
            foreach (TSourceItem item in items)
            {
                if (filter(item))
                {
                    resultItems.Add(translator(item));
                }
            }
            //Remove blanks.
            resultItems.TrimExcess();

            return(resultItems.ToArray());
        }
Exemple #2
0
        /// <summary>
        /// Alters the contents of the array by the return values indicated by the <paramref name="alterDelegate"/>.
        /// </summary>
        /// <typeparam name="TSourceItem">The type of items to alter in an array.</typeparam>
        /// <typeparam name="TDestinationItem">The type of items to return in the result array.</typeparam>
        /// <param name="items">The array to alter</param>
        /// <param name="alterDelegate">The delegate to perform the change on the elements
        /// of <paramref name="items"/>.</param>
        /// <returns></returns>
        public static TDestinationItem[] TranslateArray <TSourceItem, TDestinationItem>(TSourceItem[] items, Common.TranslateArgument <TDestinationItem, TSourceItem> alterDelegate)
        {
            List <TDestinationItem> resultItems = new List <TDestinationItem>(items.Length);

            foreach (TSourceItem item in items)
            {
                resultItems.Add(alterDelegate(item));
            }
            return(resultItems.ToArray());
        }