CSharp examples for System.Collections.Generic:ICollection
Return the enumerable as a Collection of T, copying if required.
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. using System.Linq; using System.Diagnostics.Contracts; using System.Collections.ObjectModel; using System.Collections.Generic; using System;//ww w .j av a2 s. com public class Main{ /// <summary> /// Return the enumerable as a Collection of T, copying if required. Optimized for the common case where it is /// a Collection of T and avoiding a copy if it implements IList of T. Avoid mutating the return value. /// </summary> public static Collection<T> AsCollection<T>(this IEnumerable<T> enumerable) { Contract.Assert(enumerable != null); Collection<T> collection = enumerable as Collection<T>; if (collection != null) { return collection; } // Check for IList so that collection can wrap it instead of copying IList<T> list = enumerable as IList<T>; if (list == null) { list = new List<T>(enumerable); } return new Collection<T>(list); } }