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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:19:30+00:00 2026-05-24T00:19:30+00:00

I’m reading lock Statement (C# Reference) where I saw this code: class Account {

  • 0

I’m reading lock Statement (C# Reference) where I saw this code:

class Account
{
    private Object thisLock = new Object();

    //...

    int Withdraw(int amount)
    {
        lock (thisLock)
        {
           //....
        }
    }

    //...
}

I’m wondering if it would make any difference if we write lock(this) instead of lock(thisLock) in the above example. Please see the complete example if your answer depends on it.

If you think there is indeed some difference between lock(this) and lock(thisLock) , then please help me understanding the difference with all important points. In particular, what does each exactly mean?

  • 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-24T00:19:31+00:00Added an answer on May 24, 2026 at 12:19 am

    The difference stems from the fact that lock, if misused, can result in thread deadlock. If the lock target’s visibility is unknown to you (i.e., you cannot be 100% certain who has a reference to the target and if/when they might lock it), then you cannot really know if the application might experience a deadlock.

    For this reason it’s customary to lock on a private member: since it’s private and it’s in your code, you do know that noone else can lock it.

    Of course all this is a purely academic difference most of the time (usually people don’t go around locking random objects), but it’s a good defensive coding practice.

    The page you link to states:

    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.

    Because someone else might lock the instance using a reference they have, and your code that does lock(this) will of course not expect that. Example on IDEone (see line 26).

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

    A variation of the above, if the type is visible to other code then you might end up trying to lock the same instance as that code (typeof returns singleton instances).

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

    Another variation: due to string interning, code ends up trying to lock the same instance.

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

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

Sidebar

Related Questions

No related questions found

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.