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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:05:11+00:00 2026-05-25T16:05:11+00:00

.Net List class isn’t thread safe. I hope to achieve the minimal lock needed

  • 0

.Net List class isn’t thread safe. I hope to achieve the minimal lock needed and yet still fulfilling the requirement such that as for reading, phantom record is allowed, and for writing, they must be thread-safe so there won’t be any lost updates.

So I have something like

public static List<string> list = new List<string>();

In Methods that have **List.Add**/**List.Remove** , I always lock to assure thread safety

            lock (lockHelper)
            {
                    list.Add(obj);
                    or list.Remove(obj);
            }

In Methods that requires **List Reading** I don't care about phantom record so I go ahead to read without any locking. In this case. Return a bool by checking whether a string had been added.

          if (list.Count() != 0) {
              return list.Contains("some string")
          }

All I did was locking write accesses, and allow read accesses to go through without any locking. Is my thread safety idea valid?

I understand there is List size expansion. Will it be ok? My guess is that when a List is expanding, it may uses a temp. list. This is ok becasue the temp list size will always have a boundary, and .Net class is well implemented, ie. there shouldn’t be any indexOutOfBound or circular reference problems when reading was caught in updates.

  • 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-25T16:05:12+00:00Added an answer on May 25, 2026 at 4:05 pm

    No that is not safe. You should protect against reads and enumerations.

    Since you are using 4.0, check out the new thread safe collections.

    http://msdn.microsoft.com/en-us/library/dd997305.aspx

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

Sidebar

Related Questions

I have an ASP.NET WebService that returns an object of List public class Students
I'm deserializing a class called Method using .NET Serialization. Method contains a list of
Given the following vb.net class: Friend Class PairCollection(Of TKey, TValue) Inherits List(Of KeyValuePair(Of TKey,
Is it possible in .NET to list files on a remote location like an
I've got an arbitrary list of .NET assemblies. I need to programmatically check if
I am using a asp.net drop down list and it's pulling in a collection
I'm working on an ASP.NET MVC task list and I'd like to get fancy
I have the list box control (ASP.NET Control On aspx page, language C# ).
In .NET, both array and list have Enumerable as ancestor, so a method that
I have a asp.net page where i query a list of url and the

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.