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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:26:13+00:00 2026-05-13T17:26:13+00:00

I have a class that maintains a private Dictionary instance that caches some data.

  • 0

I have a class that maintains a private Dictionary instance that caches some data.

The class writes to the dictionary from multiple threads using a ReaderWriterLockSlim.

I want to expose the dictionary’s values outside the class.
What is a thread-safe way of doing that?

Right now, I have the following:

public ReadOnlyCollection<MyClass> Values() {
    using (sync.ReadLock())
        return new ReadOnlyCollection<MyClass>(cache.Values.ToArray()); 
}

Is there a way to do this without copying the collection many times?

I’m using .Net 3.5 (not 4.0)

  • 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-13T17:26:14+00:00Added an answer on May 13, 2026 at 5:26 pm

    EDIT: I personally believe the below code is technically answering your question correctly (as in, it provides a way to enumerate over the values in a collection without creating a copy). Some developers far more reputable than I strongly advise against this approach, for reasons they have explained in their edits/comments. In short: This is apparently a bad idea. Therefore I’m leaving the answer but suggesting you not use it.


    Unless I’m missing something, I believe you could expose your values as an IEnumerable<MyClass> without needing to copy values by using the yield keyword:

    public IEnumerable<MyClass> Values {
        get {
            using (sync.ReadLock()) {
                foreach (MyClass value in cache.Values)
                    yield return value;
            }
        }
    }
    

    Be aware, however (and I’m guessing you already knew this), that this approach provides lazy evaluation, which means that the Values property as implemented above can not be treated as providing a snapshot.

    In other words… well, take a look at this code (I am of course guessing as to some of the details of this class of yours):

    var d = new ThreadSafeDictionary<string, string>();
    
    // d is empty right now
    IEnumerable<string> values = d.Values;
    
    d.Add("someKey", "someValue");
    
    // if values were a snapshot, this would output nothing...
    // but in FACT, since it is lazily evaluated, it will now have
    // what is CURRENTLY in d.Values ("someValue")
    foreach (string s in values) {
        Console.WriteLine(s);
    }
    

    So if it’s a requirement that this Values property be equivalent to a snapshot of what is in cache at the time the property is accessed, then you’re going to have to make a copy.

    (begin 280Z28): The following is an example of how someone unfamiliar with the “C# way of doing things” could lock the code:

    IEnumerator enumerator = obj.Values.GetEnumerator();
    MyClass first = null;
    if (enumerator.MoveNext())
        first = enumerator.Current;
    

    (end 280Z28)

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

Sidebar

Ask A Question

Stats

  • Questions 423k
  • Answers 423k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Build configiuration setting needs to be an "iOS Device" (or… May 15, 2026 at 11:29 am
  • Editorial Team
    Editorial Team added an answer Any of the other switch blocks will just hit the… May 15, 2026 at 11:29 am
  • Editorial Team
    Editorial Team added an answer Your method is parameterized by T - the idea is… May 15, 2026 at 11:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.