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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:39:50+00:00 2026-06-15T01:39:50+00:00

Can someone explain, in detail, why it’s possible to lock on objects of any

  • 0

Can someone explain, in detail, why it’s possible to lock on objects of any type in C#?

I understand what lock is for and how to use it. I know how it expands to Monitor.Enter/Exit. What I’m looking for an explanation of the implementation detail and design considerations.

First of all: What is happening under the hood? For example: are there extra bits in an object instance (like there is for RTTI/vtable) that make it work? Or some kind of lookup table keyed on object references? (If so, how does this interact with the GC?) Or something else? Why don’t I have to create an instance of a specific type to hold whatever the lock data is?

(And, by the way, what do Enter and Exit map to in native code?)

And, secondly, why is .NET designed to not have a specific type for taking out locks on? (Given that you generally just make a new object() for the purpose anyway – and most cases where you lock “any old object” are problematic.) Was this design choice forced by the implementation detail? Or was it deliberate? And, if deliberate, was it a good choice? (I realise this second part may require speculation.)

  • 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-15T01:39:51+00:00Added an answer on June 15, 2026 at 1:39 am

    It is possible to lock on all non struct types. In layout of each refernce type on the heap there is special field (sync block) that is used to manage lock. The layout is covered in details in How CLR Creates Runtime Object. Excerpt from article is below:

    The OBJECTREF does not point to the beginning of the Object Instance but at a DWORD offset (4 bytes). The DWORD is called Object Header and holds an index (a 1-based syncblk number) into a SyncTableEntry table.

    Object layout on the heap:

    sync block index 
    pointer to type
    fields...
    

    Speculation part: I believe that original guidance was to lock on whatever is convinient, but it was relatively quickly changed to having special “private object for lock” due to ease of getting external code to deadlock your methods. I think there even were classes in Framework that were impelented with locking on publically visible objects…

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

Sidebar

Related Questions

Can someone explain to me in detail when I must use each attribute: nonatomic
Can someone please explain below in detail, 1- What is the use of ActivityManager
Can someone explain why it's best use is for prototyping? Should it not be
I'm trying to understand what this is doing in each case. Can someone explain
Can someone explain Activator.CreateInstance() purpose in detail?
Can someone please explain in detail what these if statements are doing? What does
Can someone explain what this is doing please? foreach($_POST['Detail'] as $i_Detail => &$a_Detail) {
Can someone please point out some books or online resources which explain in detail
Can someone explain IPhone architecture in detail. What happens when we click on a
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B

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.