Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Convert IEnumerableT To ReadOnlyCollectionT

namespace Ngs.Collections {     // Copyright (c) Microsoft Corporation.  All rights reserved.     // This source code is made available under the terms of the Microsoft Public License (MS-PL)     using System;     using System.Collections.Generic;     using System.Collections.ObjectModel;     using System.Linq;     using System.Reflection;     using System.Text;     public static class ReadOnlyExtensions     {         internal static ReadOnlyCollection<T> ToReadOnly<T>(this IEnumerable<T> collection)         {             ReadOnlyCollection<T> roc = collection as ReadOnlyCollection<T>;             if (roc == null)             {                 if (collection == null)                 {                     roc = EmptyReadOnlyCollection<T>.Empty;                 }                 else                 {                     roc = new List<T>(collection).AsReadOnly();                 }             }             return roc;         }         class EmptyReadOnlyCollection<T>         {             internal static readonly ReadOnlyCollection<T> Empty = new List<T>().AsReadOnly();         }     } }