Exemplo n.º 1
0
        internal static IReadOnlyCollection <T> ReifyCollection <T>(IEnumerable <T> source)
        {
            if (source is IReadOnlyCollection <T> result)
            {
                return(result);
            }
            if (source is ICollection <T> collection)
            {
                return(new CollectionWrapper <T>(collection));
            }
            if (source is ICollection nongenericCollection)
            {
                return(new NongenericCollectionWrapper <T>(nongenericCollection));
            }

            var builder = new LargeArrayBuilder <T>(initialize: true);

            builder.AddRange(source);
            return(builder.ToArray());
        }
Exemplo n.º 2
0
        /// <summary>Converts an enumerable to an array.</summary>
        /// <param name="source">The enumerable to convert.</param>
        /// <returns>The resulting array.</returns>
        internal static T[] ToArray <T>(IEnumerable <T> source)
        {
            Debug.Assert(source != null);

            if (source is ICollection <T> collection)
            {
                int count = collection.Count;
                if (0u >= (uint)count)
                {
                    return(EmptyArray <T> .Instance); // Array.Empty<T>();
                }

                var result = new T[count];
                collection.CopyTo(result, arrayIndex: 0);
                return(result);
            }

            var builder = new LargeArrayBuilder <T>(initialize: true);

            builder.AddRange(source);
            return(builder.ToArray());
        }