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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:56:21+00:00 2026-05-28T00:56:21+00:00

Alright, so here is the property I have public List<String> names{ get{ lock(_names) return

  • 0

Alright, so here is the property I have

public List<String> names{
    get{
        lock(_names)
            return _names;
       }
   set{
       lock(_names)
           _names = value
      }
}
private List<String> _names;

and now say I do a foreach on names like this

foreach(String s in names)
{
    Console.WriteLine(s);
}

My question is, is names locked through the whole foreach, or does it only lock each time s is set, then unlocks inside the foreach.

If that’s confusing, say I try to do this

foreach(String s in names)
{
    lock(names)
        Console.WriteLine(s);
}

Will I end up in a deadlock?

  • 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-28T00:56:22+00:00Added an answer on May 28, 2026 at 12:56 am

    The get_Names method will only get called once, and the lock will be over (there will be no lock) during the iteration of the items. This is probably not what you intend and you should probably go for a much granular lock, such as:

    lock (someLockObject)
    {
        foreach(String s in names)
        {
            Console.WriteLine(s);
        }
    }
    

    When you lock inside the loop, you will take many locks one after the other, which is almost certainly not what you want, since operating the foreach loop will not be atomic anymore.

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

Sidebar

Related Questions

Alright, here is the output I get: arm-none-linux-gnueabi-ld --entry=main -dynamic-linker=/system/bin/linker -rpath-link=/home/oem/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib -L/home/oem/android-ndk-r3/build/platforms/android-5/arch-arm/usr/lib -nostdlib -lstdc++
Alright, I have been doing the following (variable names have been changed): FileInputStream fis
Alright here is what I am attempting to do. I have a form written
Alright here's the situation, I have an application written in the Zend_Framework, that is
Alright, here's an odd one from an MS Access database I'm running. I have
Alright, so here is the situation... Say I have a navbar for a site,
Alright, here's the deal: I have a nice little 4Gb table called Mails on
Alright so here is my code, I get the result I want but I
alright so i have a small problem here, i have this here. - (void)alertView:(UIAlertView
Alright, I have a rather odd question here. I feel much more comfortable writing

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.