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 5930721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:35:34+00:00 2026-05-22T14:35:34+00:00

I often have classes exposing lists as ReadOnlyCollection<T> s, i.e. public class Class {

  • 0

I often have classes exposing lists as ReadOnlyCollection<T>s, i.e.

public class Class
{
  List<string> list;

  public ReadOnlyCollection<string> TheList
  {
    get { return list.AsReadOnly(); }
  }
}

What’s the best way to do this for an IDictionary<T,U> such as a SortedList<string, string>?

  • 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-22T14:35:35+00:00Added an answer on May 22, 2026 at 2:35 pm
    public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
    {
        private readonly IDictionary<TKey, TValue> sourceDictionary;
    
        public ICollection<TKey> Keys
        {
            get { return sourceDictionary.Keys; }
        }
    
        public ICollection<TValue> Values
        {
            get { return sourceDictionary.Values; }
        }
    
        public TValue this[TKey key]
        {
            get { return sourceDictionary[key]; }
            set { throw new NotSupportedException(); }
        }
    
        public int Count
        {
            get { return sourceDictionary.Count; }
        }
    
        public bool IsReadOnly
        {
            get { return true; }
        }
    
        public ReadOnlyDictionary(IDictionary<TKey, TValue> sourceDictionary)
        {
            AssertUtilities.ArgumentNotNull(sourceDictionary, "sourceDictionary");
    
            this.sourceDictionary = sourceDictionary;
        }
    
        void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
        {
            throw new NotSupportedException();
        }
    
        public bool ContainsKey(TKey key)
        {
            return sourceDictionary.ContainsKey(key);
        }
    
        bool IDictionary<TKey, TValue>.Remove(TKey key)
        {
            throw new NotSupportedException();
        }
    
        public bool TryGetValue(TKey key, out TValue value)
        {
            return sourceDictionary.TryGetValue(key, out value);
        }
    
        void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
        {
            throw new NotSupportedException();
        }
    
        void ICollection<KeyValuePair<TKey, TValue>>.Clear()
        {
            throw new NotSupportedException();
        }
    
        public bool Contains(KeyValuePair<TKey, TValue> item)
        {
            return sourceDictionary.Contains(item);
        }
    
        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            sourceDictionary.CopyTo(array, arrayIndex);
        }
    
        bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
        {
            throw new NotSupportedException();
        }
    
        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            return sourceDictionary.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)sourceDictionary).GetEnumerator();
        }
    }
    

    [Edit]
    @Simon Buchan and @Cory Nelson pointed out that it is better to use implicit interface implementation for those methods that are not supported. Updated the code accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my code I have often situations like this: public void MyMethod(string data) {
I have the following example classes in Java: public class A { } public
I have often seen an init() within the constructor of AS3 classes, sometimes even
In Ruby I have often written a number of small classes, and had a
I have a (dump) question regarding VB/C# I often use third party classes where
I often want to define new 'Exception' classes, but need to have an appropriate
I often have data in Excel or text that I need to get into
I often have to convert a retreived value (usually as a string) - and
I often have classes that are mostly just wrappers around some STL container, like
I'm working with WPF and often have the need to get the namespace and

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.