Return the enumerable as a IList of T, copying if required. Avoid mutating the return value. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Return the enumerable as a IList of T, copying if required. Avoid mutating the return value.

Demo Code

// 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;//from  ww  w .ja  v a  2 s . c o  m

public class Main{
        /// <summary>
        /// Return the enumerable as a IList of T, copying if required. Avoid mutating the return value.
        /// </summary>
        public static IList<T> AsIList<T>(this IEnumerable<T> enumerable)
        {
            Contract.Assert(enumerable != null);

            IList<T> list = enumerable as IList<T>;
            if (list != null)
            {
                return list;
            }
            return new List<T>(enumerable);
        }
}

Related Tutorials