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

  • Home
  • SEARCH
  • 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 8323069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:36:13+00:00 2026-06-08T23:36:13+00:00

Why is it a bad practice to use lock as in the following code,

  • 0

Why is it a bad practice to use lock as in the following code, I’m assuming this is a bad practice based on the answers in this SO question here

private void DoSomethingUseLess()
{
    List<IProduct> otherProductList = new List<IProduct>();
    Parallel.ForEach(myOriginalProductList, product =>
        {
           //Some code here removed for brevity
           //Some more code here :)
            lock (otherProductList)
            {
                otherProductList.Add((IProduct)product.Clone());
            }
        });
}

The answers over there mentions that it is bad practice , but they don’t say why

Note: Please ignore the usefulness of the code, this is just for example purpose and i know it is not at all useful

  • 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-08T23:36:16+00:00Added an answer on June 8, 2026 at 11:36 pm

    From the C# language reference here:

    In general, avoid locking on a public type, or instances beyond your code’s control. The common constructs lock (this), lock (typeof (MyType)), and lock ("myLock") violate this guideline:

    lock (this) is a problem if the instance can be accessed publicly.

    lock (typeof (MyType)) is a problem if MyType is publicly accessible.

    lock("myLock") is a problem because any other code in the process
    using the same string, will share the same lock.

    Best practice is to define a private object to lock on, or a private
    static object variable to protect data common to all instances.

    In your case, I would read the above guidance as suggesting that locking on the collection you will be modifying is bad practise. For example, if you wrote this code:

    lock (otherProductList) 
    {
        otherProductList = new List<IProduct>(); 
    }
    

    …then your lock will be worthless. For these reasons it’s recommended to use a dedicated object variable for locking.

    Note that this doesn’t mean your application will break if you use the code you posted. “Best practices” are usually defined to provide easily-repeated patterns that are more technically resilient. That is, if you follow best practice and have a dedicated “lock object,” you are highly unlikely to ever write broken lock-based code; if you don’t follow best practice then, maybe one time in a hundred, you’ll get bitten by an easily-avoided problem.

    Additionally (and more generally), code written using best practices is typically more easily modified, because you can be less wary of unexpected side-effects.

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

Sidebar

Related Questions

Is it considered a bad practice to use code-behind with ASP.NET MVC Views? Got
Is it generally considered bad practice to use typeid in production code? Also, I
Is it bad practice to use the following format when my_var can be None?
Is this bad practice to use php statements such as 'if' within a jquery
Is it bad practice to use the instanceof operator in the following context? public
Consider the following test case, is it a bad practice to use the hashCode
I know it is bad practice to use equality instead of equivalence when sorting
1) Isn't it a bad practice to use these attributes on all of my
In an MVC3 application, is it considered bad practice to use a try catch
Is the code below bad practice or undefined behavior? Essentially i am calling a

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.