Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4607386
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:41:46+00:00 2026-05-22T00:41:46+00:00

I thought IEnumerable things are objects over which you can iterate. If they are

  • 0

I thought IEnumerable things are objects over which you can iterate.
If they are also ICollections you know how much elements are in there.
And if they are even ILists you can take containing objects from a specific index.

A ReadOnlyCollection<T> implements IList<T>. So wouldnt ReadOnlyList<T> be a better name.

And is there a real ReadOnlyCollection<T> in the framework?
(So I dont need an IList to create such a read only wrapper)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:41:46+00:00Added an answer on May 22, 2026 at 12:41 am
    namespace System.Collections.ObjectModel
    {
        using System.Collections.Generic;
        using System.Diagnostics;
        using System.Runtime.InteropServices;
        using System.Threading;
    
        internal sealed class CollectionDebugView<T>
        {
            private readonly ICollection<T> collection;
    
            public CollectionDebugView(ICollection<T> collection)
            {
                if (collection == null) { throw new ArgumentNullException("collection"); }
    
                this.collection = collection;
            }
    
            [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
            public T[] Items
            {
                get
                {
                    T[] items = new T[this.collection.Count];
                    this.collection.CopyTo(items, 0);
                    return items;
                }
            }
        }
    
        [Serializable]
        [ComVisible(false)]
        [DebuggerDisplay("Count = {Count}")]
        [DebuggerTypeProxy(typeof(CollectionDebugView<>))]
        public class ReadOnlyCollection<T> : ICollection<T>, ICollection
        {
            private readonly ICollection<T> genericCollection;
            private readonly ICollection nongenericCollection;
    
            [NonSerialized]
            private object syncRoot;
    
            public ReadOnlyCollection(ICollection<T> collection)
            {
                if (collection == null) { throw new ArgumentNullException("collection"); }
    
                this.genericCollection = collection;
                this.nongenericCollection = collection as ICollection;
            }
    
            void ICollection<T>.Add(T item)
            {
                throw new NotSupportedException();
            }
    
            void ICollection<T>.Clear()
            {
                throw new NotSupportedException();
            }
    
            public bool Contains(T item)
            {
                return this.genericCollection.Contains(item);
            }
    
            public void CopyTo(T[] array, int arrayIndex)
            {
                this.genericCollection.CopyTo(array, arrayIndex);
            }
    
            public int Count
            {
                get { return this.genericCollection.Count; }
            }
    
            public bool IsReadOnly
            {
                get { return true; }
            }
    
            bool ICollection<T>.Remove(T item)
            {
                throw new NotSupportedException();
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                return this.genericCollection.GetEnumerator();
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return ((IEnumerable)this.genericCollection).GetEnumerator();
            }
    
            void ICollection.CopyTo(Array array, int index)
            {
                if (this.nongenericCollection == null)
                {
                    if (array == null) { throw new ArgumentNullException("array"); }
                    if (array.Rank != 1) { throw new ArgumentException(); }
                    if (array.GetLowerBound(0) != 0) { throw new ArgumentException(); }
                    if (index < 0) { throw new ArgumentOutOfRangeException(); }
                    if (array.Length < index + this.genericCollection.Count) { throw new ArgumentException(); }
    
                    T[] typedArray = array as T[];
    
                    if (typedArray == null)
                    {
                        Type arrayType = array.GetType().GetElementType();
                        Type collectionType = typeof(T);
    
                        if (arrayType.IsAssignableFrom(collectionType) || collectionType.IsAssignableFrom(arrayType))
                        {
                            object[] objectArray = array as object[];
    
                            if (objectArray == null) { throw new ArgumentException(); }
    
                            try
                            {
                                foreach (T item in this.genericCollection)
                                {
                                    objectArray[index] = item;
                                    index++;
                                }
                            }
                            catch (ArrayTypeMismatchException)
                            {
                                throw new ArgumentException();
                            }
    
                        }
                        else
                        {
                            throw new ArgumentException();
                        }
                    }
                    else
                    {
                        this.genericCollection.CopyTo(typedArray, index);
                    }
                }
                else
                {
                    this.nongenericCollection.CopyTo(array, index);
                }
            }
    
            int ICollection.Count
            {
                get { return this.nongenericCollection == null ? this.genericCollection.Count : this.nongenericCollection.Count; }
            }
    
            bool ICollection.IsSynchronized
            {
                get { return this.nongenericCollection == null ? false : this.nongenericCollection.IsSynchronized; }
            }
    
            object ICollection.SyncRoot
            {
                get
                {
                    if (this.syncRoot == null)
                    {
                        if (this.nongenericCollection == null)
                        {
                            this.syncRoot = this.nongenericCollection.SyncRoot;
                        }
                        else
                        {
                            Interlocked.CompareExchange<object>(ref this.syncRoot, new object(), null);
                        }
                    }
    
                    return this.syncRoot;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought IQueryable <T> was derrived from IEnumerable <T> , so why can't I
Let's say I have collection with 100 elements. Regular enumerator would iterate over those
I thought that there was some way in .net 3.0 to give an array
I have an IEnumerable full of objects that we are using to represent user
Yes, I know, yet another question about mutable objects. See this for general background
Is there a limit to the rows that IEnumerable.Count() (or IQueryable.Count()) using LINQ to
When trying to obtain an array of objects from an IEnumerable collection of objects
I've got a wierd/annoying little bug which I don't even know how to properly
I have a List/IEnumerable of objects and I'd like to perform a calculation on
Is there such a thing as a thing that returns an IEnumerable<object> or IEnumerable<T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.