示例#1
0
        /// <summary>
        /// Create a filtered array.
        /// </summary>
        public static T[] Where <T>(this T[] array, Func <T, bool> predicate)
        {
            if ((array == null) || (array.Length == 0))
            {
                return(array);
            }
            ArrayList <T> list = null;

            foreach (var item in array)
            {
                if (predicate(item))
                {
                    if (list == null)
                    {
                        list = new ArrayList <T>(array.Length);
                    }
                    list.Add(item);
                }
            }
            var elementType = array.GetType().GetElementType();

            if (list == null)
            {
                return((T[])Array.NewInstance(elementType, 0));
            }
            return(list.ToArray((T[])Array.NewInstance(elementType, list.Count)));
        }
示例#2
0
        public static T[] Take <T>(this T[] array, int count)
        {
            var len = Math.Min(count, array.Length);

            T[] ret = new T[len];
            System.Array.Copy(array, ret, len);
            return(ret);
        }
示例#3
0
        public static T FirstOrDefault <T>(this T[] array)
        {
            int len = array.Length;

            if (len == 0)
            {
                return(default(T));
            }
            return(array[0]);
        }
示例#4
0
        public static T FirstOrDefault <T>(this T[] array, Func <T, bool> predicate)
        {
            int len = array.Length;

            for (int i = 0; i < len; ++i)
            {
                if (predicate(array[i]))
                {
                    return(array[i]);
                }
            }
            return(default(T));
        }
示例#5
0
        public static TOut[] Select <TIn, TOut>(this TIn[] array, Func <TIn, TOut> select)
        {
            if ((array == null) || (array.Length == 0))
            {
                return(new TOut[0]);
            }

            var ret = new TOut[array.Length];

            for (int i = 0; i < array.Length; ++i)
            {
                ret[i] = select(array[i]);
            }

            return(ret);
        }
示例#6
0
        public static T Last <T>(this T[] array)
        {
            int len = array.Length;

            return(array[len - 1]);
        }