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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:50:38+00:00 2026-06-09T14:50:38+00:00

First let me explain why I’m using a KeyedCollection. I’m building a DLL and

  • 0

First let me explain why I’m using a KeyedCollection. I’m building a DLL and I have a list of items that I need to add to a collection and have them stay in the order I placed them but I also need to access them by both their index and by key (the key is a property of an object which I already defined). If there is any other simpler collection that does this, then please let me know.

Ok now, I need to be able to add items to this collection internally in the DLL but I need it to be publicly available to the DLL’s end-user as read-only because I don’t want them removing/altering the items I added.

I’ve searched all over this site, other sites, google in general and I have not been able to find a way to get some sort of read-only KeyedCollection. The closest I came was this page (http://www.koders.com/csharp/fid27249B31BFB645825BD9E0AFEA6A2CCDDAF5A382.aspx?s=keyedcollection#L28) but I couldn’t quite get it to work.

UPDATE:

I took a look at those C5 classes. That, along with your other comments, helped me better understand how to create my own read-only class and it seems to work. However I have a problem when I try to cast the regular one to the read-only one. I get a compile-time cannot convert error. Here’s the code I created (the first small class is what I originally had):

public class FieldCollection : KeyedCollection<string, Field>
{
    protected override string GetKeyForItem(Field field)
    {
        return field.Name;
    }
}

public class ReadOnlyFieldCollection : KeyedCollection<string, Field>
{
    protected override string GetKeyForItem(Field field)
    { return field.Name; }

    new public void Add(Field field)
    { throw new ReadOnlyCollectionException("This collection is read-only."); }

    new public void Clear()
    { throw new ReadOnlyCollectionException("This collection is read-only."); }

    new public void Insert(int index, Field field)
    { throw new ReadOnlyCollectionException("This collection is read-only."); }

    new public bool Remove(string key)
    { throw new ReadOnlyCollectionException("This collection is read-only."); }

    new public bool Remove(Field field)
    { throw new ReadOnlyCollectionException("This collection is read-only."); }

    new public bool RemoveAt(int index)
    { throw new ReadOnlyCollectionException("This collection is read-only."); }
}

If I have this variable defined:

private FieldCollection _fields;

then do this:

public ReadOnlyFieldCollection Fields;
Fields = (ReadOnlyFieldCollection)_fields;

it fails to compile. They’re both inheriting from the same class, I thought they would be “compatible”. How can I cast (or expose) the collection as the read-only type I just created?

  • 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-06-09T14:50:39+00:00Added an answer on June 9, 2026 at 2:50 pm

    I don’t know of any built-in solution as well. This is an example of the suggestion I gave on the comment:

        public class LockedDictionary : Dictionary<string, string>
        {
            public override void Add(string key, string value)
            {
                //do nothing or log it somewhere
                //or simply change it to private
            }
    
            //and so on to Add* and Remove*
    
            public override string this[string i]
            {
                get
                {
                    return base[i];
                }
                private set
                {
                    //...
                }
            }
        }
    

    You’ll be able to iterate through the KeyValuePair list and everything else, but it won’t be available for writing operations.
    Just be sure to type it according to your needs.


    EDIT
    In order to have a sorted list, we can change the base class from Dictionary<T,T> to a Hashtable.

    It would look like this:

        public class LockedKeyCollection : System.Collections.Hashtable
        {
            public override void Add(object key, object value)
            {
                //do nothing or throw exception?
            }
    
            //and so on to Add* and Remove*
    
            public override object this[object i]
            {
                get
                {
                    return base[i];
                }
                set
                {
                    //do nothing or throw exception?
                }
            }
        }
    

    Usage:

            LockedKeyCollection aLockedList = new LockedKeyCollection();
    
            foreach (System.Collections.DictionaryEntry entry in aLockedList)
            {
                //entry.Key
                //entry.Value
            }
    

    Unfortunately, we can’t change the access modifier of the methods, but we can override them to do nothing.

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

Sidebar

Related Questions

First let me explain. I have several addresses on the page that I put
First let me explain that I have this script that should let users enter
First of all let me explain that I have searched for at least an
Hello and let me first explain that I have not used jQuery very much
First, let me explain that I'm using Spring MVC 3.1.1, and Hibernate validation 4.2.0.
Let me first explain what's I'm trying to do. So I have a entity
First let me say I have read this useful article thoroughly and am using
First let me explain that I've created a million lib directories scouring out all
Let me first explain what I'm trying to achieve using some pseudo-code (JavaScript). //
That's the first time i get an error like this one, let me explain

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.