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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:10:33+00:00 2026-06-17T10:10:33+00:00

Am I right in saying that I only need to use lock to Add/Remove/Change

  • 0

Am I right in saying that I only need to use lock to Add/Remove/Change the List, or do I also need to lock it when iterating over it?

So am I thread safe by doing this:

class ItemsList
{
    List<int> items = new List<int>();
    object listLock = new object();

    public void Add(int item)
    {
        lock (listLock)
        {
            items.Add(item);
        }
    }

    public void Remove(int item)
    {
        lock (listLock)
        {
            items.Remove(item);
        }
    }

    public void IncrementAll()
    {
        foreach (var item in items)
        {
            item += 1;
        }
    }
}
  • 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-17T10:10:34+00:00Added an answer on June 17, 2026 at 10:10 am

    You should definitely lock when iterating over it too – if the list is changed while you’re iterating over it, an exception will be thrown.

    From the docs for List<T>.GetEnumerator:

    The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

    Additionally, even a single read from a List<T> isn’t thread-safe if you could be writing to it as well – even if it doesn’t fail, there’s no guarantee that you’ll get the most recent value.

    Basically, List<T> is only safe for multiple threads if it’s not written to after the last point at which its state becomes visible to all threads.

    If you want a thread-safe collection, and if you’re using .NET 4 or higher, take a look at the System.Collections.Concurrent namespace.

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

Sidebar

Related Questions

I know from this thread - template class member function only specialization that if
I'm counting the right answers field of a table and saving that calculated value
I want to preface this by saying I believe the Right™ Way to handle
Right now I have a script that will get the last five files in
Right now I have a function, in a class that is used to listen
I don't even know if that's the right term. May it be known that
So i have a problem regarding usage of sharepoint list webservices. I need to
My upload servlet keeps throwing me an exception saying that the file that I'm
I have an android application that consists of a WebWiew and I need to
I have a TODO file that I load emacs up to use 90% of

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.